Page 1 of 1

counting instances of a character in a string problem

Posted: Mon Oct 14, 2002 4:34 pm
by levi_501_dehaan
Aloha,
I need to count the instances of a string.

$result = (count_chars($line, 0));
$countedcom = sizeof($result[","]);
$countedcom = $countedcom - 5;
$counts = $countedcom / 5;

i need to count how many commas there are in the string, then subtract that by 5 then divide by 5,this code is not working. i need to get it working :). any help would be great.
-=Levi=-

Posted: Mon Oct 14, 2002 4:45 pm
by hob_goblin
try something like,

Code: Select all

<?
$string = ",,,,";
preg_match_all("(,)", $string, $out);
echo count($out&#1111;'0']);
?>

hmmm.

Posted: Mon Oct 14, 2002 5:06 pm
by levi_501_dehaan
Well maybe I need to post a bit more on the problem :)

alrighty, i have a very huge file, full of data,
i chopped the data up into smaller files according to their location.
then i load that data, and i chop individual pieces up into an array,
using /n with the explode function that data looks like this before.

SEX,Frequency,Percent,ValidPercent,CumulativePercent,Valid,F,627,60.5,60.5,60.5,M,409,39.4,39.4,99.9,uk,1,.1,.1,100.0,Total,1037,100.0,100.0,/nRES,Frequency,Percent,ValidPercent,CumulativePercent,Valid,INSTATE,416,40.1,40.2,40.2,OUTOFSTATE,155,14.9,15.0,55.1,INDISTRICT,299,28.8,28.9,84.0,NONCREDIT,166,16.0,16.0,100.0,Total,1036,99.9,100.0,Missing,System,1,.1,Total,1037,100.0,/n

i use strpos and seperate the data up using the commas as seperators.
see that the first thing is the table name in these two cases SEX and RES. then the field names are the static frequency, percent, validpercent, cumulativepercent, and valid.
i then use a loop to extract the rest of the data, and put it into an array, which is then put into a database using another loop to extract the data from the array.
only problem, is i need to count the commas from start to the /n which is string 0 in the first array run through the program. it is 25 commas in the first set of data, i need to subtract 5 from that because those are the commas seperating the table name and the field names, then i need to divide by 5 and get the number for the loop which puts the data into an array, and then for the loop which puts the data into a database from the array.
I doubt i made much sense, since i am running on only coffee, and nothing else, whooo i can feel my brain expanding already 8). so if you want i can post the sections of code that pertain to this so you can better understand what i am trying to get at here 8).
and if you have any ideas that may help make this faster, Please let me know 8)
-=Levi=-

Posted: Mon Oct 14, 2002 5:09 pm
by hob_goblin
Post the code, it would help, i think im getting it though.

Posted: Mon Oct 14, 2002 5:24 pm
by levi_501_dehaan
alrighty, I think I am onto my problem here, i dont think i need to store my data into arrays if it is going to be looping through each string by string. but it is still not working, i removed the arrays, and the comma counter and such, but something is still not working.
Here's the code:

if(isset($serveraddy) && isset($databasename) && isset($username) && isset($passwd))
{
$h = 1;
while($h <= $manyfiles)
{
$i = 0; //Counter

for($p = 0; $p < $manyfiles; $p++)
{
if(isset($filearray[$p]))
{
echo "is set ".$filearray[$p]."";
}else{
echo "is not set";
}
}
for ($i = 0; $i < $manyfiles; $i++)
{
$fd = fopen($filearray1[$i], "r+b");
$xfsize = filesize($filearray1[$i]);

if(!isset($fd)){echo"no filename";}
while(!feof($fd))
{
$linez = fread($fd, $xfsize);
$lined = explode("/n", $linez);
$countt = count($lined);
//echo "<br>1:".$linez."<br>";
echo "<br>2:".$countt."<br>";
echo "<br>3:".$lined[0]."<br>";
for($t = 0; $t < $countt ; $t++)
{
$line = $lined;
//if(isset($lined)) {echo $lined;} else {echo "no data from lined";)

$len_line = strlen($line); // Lenght of line

//get table name
$tablenamecomma = strpos($line, ",", 0); //Position first comma
$tablename = substr($line, 0, $tablenamecomma); //Cut string, name of table
//get field names
$fieldname1 = strpos($line, ",", $tablenamecomma + 1); //Position second comma
$fieldname2 = strpos($line, ",", $fieldname1 + 1); //Position third comma
$fieldname3 = strpos($line, ",", $fieldname2 + 1); //Position fourth comma
$fieldname4 = strpos($line, ",", $fieldname3 + 1); //Position fourth comma
$fieldname5 = strpos($line, ",", $fieldname4 + 1); //Position fourth comma
//get data identifier
$datastar = $fieldname5;
echo "datastar =".$datastar;
$datanameset = strpos($line, ",", $datastar + 1);
//get data
$dataname1 = strpos($line, ",", $dataset + 1);
$dataname2 = strpos($line, ",", $data1 + 1);
$dataname3 = strpos($line, ",", $data2 + 1);
$dataname4 = strpos($line, ",", $data3 + 1);
$dataname5 = strpos($line, ",", $data4 + 1);
$datastar = $dataname5;



$field1 = substr($line, $tablenamecomma + 1, $fieldname1 - $tablenamecomma -1); //Cut the first fieldname
$field2 = substr($line, $fieldname1 + 1, $fieldname2 - $fieldname1 - 1); //Cut the second fieldname
$field3 = substr($line, $fieldname2 + 1, $fieldname3 - $fieldname2 - 1); //Cut the third fieldname
$field4 = substr($line, $fieldname3 + 1, $fieldname4 - $fieldname3 - 1); //Cut the fourth fieldname
$field5 = substr($line, $fieldname4 + 1, $fieldname5 - $fieldname4 - 1); //Cut the fourth fieldname
$deathstar = $fieldname5;
$g = 1;

$dataset = substr($line, $deathstar + 1, $datanameset - $deathstar -1);
$data1 = substr($line, $dataset + 1, $dataname1 - $dataset -1);
$data2 = substr($line, $dataname1 + 1, $dataname2 - $dataname1 -1);
$data3 = substr($line, $dataname2 + 1, $dataname3 - $dataname2 -1);
$data4 = substr($line, $dataname3 + 1, $dataname4 - $dataname3 -1);
$data5 = substr($line, $dataname4 + 1, $dataname5 - $dataname4 -1);
$deathstar = $dataname5;


//Function to create Database, Tables, and fields if they dont exist. fun stuff eh?....no...its not.
$global_db = mysql_connect($serveraddy, $username, $passwd);
//Database function !database ? create one : fail
$db_list = mysql_list_dbs();
$cnt = mysql_num_rows($db_list);
if($t < $cnt)
{
if(mysql_db_name($db_list, $t) == $databasename)
{
$databasenametrue = 1;
mysql_select_db($databasename, $global_db);
if(isset($createtable)){$createtable++;}
}
if($databasenametrue !== 1)
{
mysql_create_db($databasename);
mysql_select_db($databasename, $global_db);
$databasenametrue = 1;
$createtable = 0;
}
}
//end of database function
//table function !table ? create one :fail
/*
$tablel = mysql_list_tables($databasename, $global_db);
for ($b = 0; $b < mysql_num_rows($tablel); $b++)
{
$tablelist[$b] = mysql_tablename($table1, $b);
// $tablelist[$b] == $tablename ? : $createtableb = 1;
}
echo $tablename." is the name of the table"*/

$result = mysql_list_tables($databasename);
if($createtable == $t && !mysql_tablename($result, $t))
{
$sql = "CREATE TABLE ".$tablename." (ID INT NOT NULL AUTO_INCREMENT PRIMARY KEY, dataset TEXT, ".$field1." TEXT, ".$field2." TEXT, ".$field3." TEXT, ".$field4." TEXT, ".$field5." TEXT)";
if(mysql_query($sql)) { echo("<P>".$tablename." table successfully created!</P>");} else { echo("<P>Error creating ".$tablename." table: ".mysql_error()."</P>");}
}
//end of table function


$query = "INSERT INTO ".$tablename." (dataset,".$field1.", ".$field2.", ".$field3.", ".$field4.", ".$field5.") VALUES (".$dataset."".$data1.",".$data2.",".$data3.",".$data4.",".$data5.")";
$result = mysql_query($query) or die("ERROR2");

$i = $i + 1;
}
}
}
fclose($fd); //Close the file
}
++$h;
print("".$i." records have been imported succesfully!");
}

}

forget it...

Posted: Mon Oct 14, 2002 6:24 pm
by levi_501_dehaan
Hey, dont worry about this one, hopefully somone can delete this post, i just fixed my problem :). but hey thanks for the help anyways.
eventually i will find a problem that can stump me.....well... hey anything can happen, even the strangest things like millions of people asking an idiot to be their leader ;)