arunhorne has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I'm reading a file such that once a certain condition is met, I want to skip forward 6 lines in the file. I though what I'd written below would work but it doesn't... how do I do this... What I want is for the final (6th) line to be in $_ once have skipped forward... Thanks.

open DIFF, "<$diff_file" or print $query->p("Couldn't open $diff_file" +); while (<DIFF>) { if (m/Cluster (\d+)/) { my $num = $1; # Read 6 lines; <DIFF>;<DIFF>;<DIFF>;<DIFF>;<DIFF>;<DIFF>; #my @ecs = split /, /; $clust_hash{$num} = $_;#\@ecs; } } close DIFF;
____________
Arun

Replies are listed 'Best First'.
Re: skip forward through file
by Abigail-II (Bishop) on Jul 04, 2002 at 13:05 UTC
    open DIFF, $diff_file or die $!; OUTER: while (<DIFF>) { if (/Cluster (\d+)/) { my $num = $1; foreach my $c (1 .. 6) { last OUTER unless defined ($_ = <DIFF>); } $clust_hash {$num} = $_; } }
•Re: skip forward through file
by merlyn (Sage) on Jul 04, 2002 at 14:35 UTC
    As an explanation for the working code already published elsewhere in this thread, perhaps what's missing in your code is that you had presumed that:
    <DIFF>;
    automatically assigns to $_. No such thing.

    I'm curious about what you read or how you figured that false fact out, because it's not said in any official documentation (in fact, the official documentation specifically contradicts it in a few places). And yet I've seen more than one person make that false attempt.

    If you read it somewhere, can you post that reference so we can track down the bad meme?

    Thanks!

    -- Randal L. Schwartz, Perl hacker

      Well, I can come up with a reason why many people think so: because the standard idiom of:
      while (<DIFF>)
      assigns to $_. And for a lot of people, after their introduction course, the only way the remember to iterate over a file is the above idiom.

      Abigail