Connecting Tech Pros Worldwide Help | Site Map

Printing only two lines from a file

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 17th, 2005, 01:36 AM
Fernando Alvarez-Uria
Guest
 
Posts: n/a
Default Printing only two lines from a file

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, 01:36 AM
Pedro Graca
Guest
 
Posts: n/a
Default 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, 01:36 AM
Jedi121
Guest
 
Posts: n/a
Default 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/

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.