Beefy Boxes and Bandwidth Generously Provided by pair Networks vroom
Just another Perl shrine
 
PerlMonks  

skip forward through file

by arunhorne (Pilgrim)
on Jul 04, 2002 at 08:44 UTC ( [id://179446]=perlquestion: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.

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 09: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 10: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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://179446]
Approved by wil
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.