Printing only two lines from a file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Fernando Alvarez-Uria

    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("f ile01");

    $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.in c on line 172


    Does anyone knows how to do it in another way?

    Thanks a lot.

  • Pedro Graca

    #2
    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 =--

    Comment

    • Jedi121

      #3
      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(fi le("file01"));
      Then parse $resultarray :
      foreach ($resultarray AS $key => $val) {
      echo "Distinct line number $key = $val<br>\n";
      }

      See the doc :


      --
      Have you read the manual?


      Comment

      Working...