Connecting Tech Pros Worldwide Help | Site Map

Printing only two lines from a file

  #1  
Old July 17th, 2005, 02:36 AM
Fernando Alvarez-Uria
Guest
 
Posts: n/a
Hi all!

I have a file (file01, i.e) like this:

foo
foo
foo
bar
foo
bar
foo

....

Where foo and bar repeats n times, but only foo and bar. What i want to
print is just:


foo
bar


I have tried this:

$myfile=file("file01");

$current_line = reset($myfile);
$first_line = $current_line;

print("First line is: $first_line");

while ($current_line=next($myfile))
{
if ($current_line != $first_line)
{
print("The other line is: $current_line");
break;
}
}


and it works as stand alone, but in the real application, i get:

Fatal error: Cannot break/continue 1 level in
/var/www/html/web/include/functionShow.inc on line 172


Does anyone knows how to do it in another way?

Thanks a lot.

  #2  
Old July 17th, 2005, 02:36 AM
Pedro Graca
Guest
 
Posts: n/a

re: Printing only two lines from a file


Fernando Alvarez-Uria wrote:[color=blue]
> Hi all!
>
> I have a file (file01, i.e) like this:
>
> foo
> foo
> foo
> bar
> ...
>
> Where foo and bar repeats n times, but only foo and bar. What i want to
> print is just:
>
> foo
> bar[/color]
....[color=blue]
> Does anyone knows how to do it in another way?[/color]

Try this:

<?php
unset($x);
$fh = fopen('file01', 'r');
while (!feof($fh)) { // read the file line-by-line
$line = trim(fgets($fh));
if ($line) $x[$line]++; // and increment a count for each line
}
fclose($fh);
echo "lines in file (hopefully by order of appearance):\n------------\n";
foreach ($x as $line=>$count) {
echo "[$line] shows up $count times\n"; // display the line and count
// echo "$line\n"; // display just the line
}
echo "------------\n";
?>
--
--= my mail box only accepts =--
--= Content-Type: text/plain =--
--= Size below 10001 bytes =--
  #3  
Old July 17th, 2005, 02:36 AM
Jedi121
Guest
 
Posts: n/a

re: Printing only two lines from a file


"Fernando Alvarez-Uria" a écrit le 12/12/2003 :[color=blue]
> Hi all![/color]
Hi
[color=blue]
> I have a file (file01, i.e) like this:
> foo
> foo
> foo
> bar
> foo
> bar
> foo
> ...
>
> Where foo and bar repeats n times, but only foo and bar. What i want to print
>
> I have tried this:[/color]
[...][color=blue]
> Does anyone knows how to do it in another way?[/color]

Try to use array_unique on your $myfile var :
$resultarray = array_unique(file("file01"));
Then parse $resultarray :
foreach ($resultarray AS $key => $val) {
echo "Distinct line number $key = $val<br>\n";
}

See the doc :
http://fr.php.net/manual/en/function.array-unique.php

--
Have you read the manual?
http://www.php.net/manual/en/

Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to prevent DISTILLER from propting output filename ? tommaso.gastaldi@uniroma1.it answers 6 June 21st, 2006 02:55 AM
how to count rows and columns of integers/doubles in a file? Martin Joergensen answers 68 April 3rd, 2006 03:25 AM
File Viewer that stops at the 24th line Rafael answers 7 November 13th, 2005 11:56 PM
Is it OK to post small image attachments here? Lauren Wilson answers 49 November 13th, 2005 05:07 AM