This is a discussion on Newbie: How to loop through lines in a file? within the PHP Language forums, part of the PHP Programming Forums category; I have an array ($data_array) and I want to compare each line of a text file ($counter_file) to each element ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
I have an array ($data_array) and I want to compare each line of a text file
($counter_file) to each element of the array (the array and the file are in sync line-for-line). But the file is very big and I don't want to read the entire file into an array. In the interest of efficiency, I thought I'd open the file as read only and loop through and compare each line to the array - since I have to open the file later in append mode, I thought something like this would work. But how do I advance the pointer to the next line in the file as the loop increments? $fp = fopen($counter_file, "r"); for (i=0; i < count($data_array); i++) { $recent_visit = fgets($fp); //<<== * ? // * how to go to next line as i increments? if ($recent_visit < TIME1h) { $dup_line = explode("|", $data_array[i]) $dup_ip = trim($dup_line[i]); if ($visip == $dup_ip) //no dupes within one hour { exit; } } else { break; } } $fp = fopen($counter_file, "a"); [append stuff to counter_file here] Thanks in advance! |
|
|||
|
deko wrote:
> I have an array ($data_array) and I want to compare each line of a text file > ($counter_file) to each element of the array (the array and the file are in > sync line-for-line). But the file is very big and I don't want to read the > entire file into an array. The file function automatically returns the named file as an array broken on each line in the file. By using it you can set up a for loop equal to the length of the test array and compare the corresponding line in the file. It might look like this: $TheFile = file("path/to/file"); for($i = 0; $i < count($TestArray); $i ++) { if($TestArray[$i] == $TheFile[$i]) { // Code for true. } else { // Code for false. } } Note however that PHP may choke when dealing with large files because there is a default execution time of 30 seconds or so. This can be overridden, though I don't recall how off hand. -- Scott Orsburn scottso_no@spam_maclaunch.com |
|
|||
|
Here's my take on this. It doesn't need you to load the whole file into
an array. It uses a while loop to read line by line and compares it with the first element of the data array one by one. <?php $file = 'test.txt'; $holder = file('test.txt'); if (check_lines($holder, $file)) { print "Same"; } else { print "Different"; } function check_lines ($array, $file) { $same = TRUE; $fp = fopen($file, 'r') or die("Couldn't open $file"); while (! feof($fp) ) { $tmp = array_shift($array); // get first element of array if ($tmp !== fgets($fp, 100000)) { // code if different $same = FALSE; break; } else { // code if same } } fclose($fp); return $same; } ?> Slidebay :: http://www.slidebay.com A Search Engine for presentations on the web |
|
|||
|
deko wrote:
> I have an array ($data_array) and I want to compare each line of a > text file ($counter_file) to each element of the array (the array and > the file are in sync line-for-line). But the file is very big and I > don't want to read the entire file into an array. > > In the interest of efficiency, I thought I'd open the file as read > only and loop through and compare each line to the array - since I > have to open the file later in append mode, I thought something like > this would work. But how do I advance the pointer to the next line > in the file as the loop increments? > > $fp = fopen($counter_file, "r"); > for (i=0; i < count($data_array); i++) > { > $recent_visit = fgets($fp); //<<== * ? > // * how to go to next line as i increments? The file pointer is advanced as part of the read. You should get used to using the manual at http://php.net Here's the example they give: <?php $handle = fopen("/tmp/inputfile.txt", "r"); while (!feof($handle)) { $buffer = fgets($handle, 4096); echo $buffer; } fclose($handle); ?> http://us3.php.net/manual/en/function.fgets.php Brian |
|
|||
|
Scott Orsburn wrote:
> deko wrote: > > I have an array ($data_array) and I want to compare each line of a > > text file ($counter_file) to each element of the array (the array > > and the file are in sync line-for-line). But the file is very big > > and I don't want to read the entire file into an array. > > > The file function automatically returns the named file as an array > broken on each line in the file. Which he said he didn't want to do. > Note however that PHP may choke when dealing with large files because > there is a default execution time of 30 seconds or so. This can be > overridden, though I don't recall how off hand. It also can be quite inefficient if you are looking for a particular line and want to stop execution after dealing with it. Brian |
|
|||
|
gg wrote:
>Here's my take on this. It doesn't need you to load the whole file into >an array. It uses a while loop to read line by line and compares it >with the first element of the data array one by one. > > >$file = 'test.txt'; >$holder = file('test.txt'); > Your line: $holder = file('test.txt'); just loaded the whole file into an array. That is if test.txt is your name for the file the original poster didn't want to load. And of course we don't know what a "big" file means though. In an earlier post of mine I used the file() function as well. My thoughts were that it's worthwhile since it builds an array to compare against automatically. And if the comparison fails it can return [exit] and avoid comparing the rest of the file. -- Scott Orsburn scott@orsburn.net Kaomso | Information Technology www.kaomso.com |
|
|||
|
> The file pointer is advanced as part of the read. You should get used > to using the manual at http://php.net > > Brian, As a newbie to PHP I was delighted to see you say:- "The file pointer is advanced as part of the read" A simple BUT important concept!! And NOT specifically document in any PHP books I have! It seems to be assumed but - in fact - is it documented in the manual ? I can't find it easily! I know its very basic (and implied/obvious when you follow through on the code you referenced) - but ?actually documented? Your comments appreciated Tack |
|
|||
|
> "The file pointer is advanced as part of the read"
> > A simple BUT important concept!! And NOT specifically document in any PHP > books I have! > It seems to be assumed but - in fact - is it documented in the manual ? I > can't find it easily! > I know its very basic (and implied/obvious when you follow through on the > code you referenced) - but ?actually documented? > Your comments appreciated > Tack Yes, the manual fails to indicate this - hence my post. In any case, I ended up sucking the file into an array. It needed to be done elsewhere in the script, so I figured I'd just do it here. The extra latency is imposed on cookie-rejecters only. I figure cookies are part of modern web browsing and should be accepted. Cookies can be easily deleted, so I don't see any sense in rejecting them. Web developers have it difficult enough. If someone comes to my site and rejects my cookie, they can wait a few extra milliseconds for my array to populate. |
|
|||
|
.... sorry im only catching the last 1 message of this thread... but
maybe this will help.. this opens a file = "file" and reads one $line at a time, until it reaches end of file. --START CODE-- $fres = fopen("file") while (!(feof($fres)){ $line = fgets($fres,$MAXLINESIZE); // do what ever you want to $line here... } fclose($fres); --END CODE-- sorry if its a bit curde... kinda a throw back to my good ole c days ... (last week i think). hth jv |