Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

move down 2 lines in file?

by annie06 (Acolyte)
on Jul 11, 2008 at 14:29 UTC ( [id://696975]=perlquestion: print w/replies, xml ) Need Help??

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

Hi all, What is the correct way to move down, say 2 or 3 lines in a file. For example, right now after I open my file and find a line I want, I want to move past the first line which is the heading, but then also move past the next line which is another heading..etc. Currently to move down 1 line I do something like:
open (file.....) while (<FILE>) { if /blah/ { $_ = <FILE>;

What is the correct way rather than just adding another line of '$_ <FILE>' to move down another line?

Replies are listed 'Best First'.
Re: move down 2 lines in file?
by almut (Canon) on Jul 11, 2008 at 14:55 UTC

    In case you want to get rid of the $_ = <FILE>, you could also write something like

    my $skip_n = 0; while (<FILE>) { next if $skip_n && $skip_n--; if (/blah/) { $skip_n = 2; # schedule the skipping of two lines } }
      ++

      The beauty of this approach is that you don't find your code trying to read past the end of the file just to skip lines.
      Also, if you don't know how many lines to skip but you *do* know what the first line-of-interest starts with, you can say something like (untested):
      my $good_start = 0; while (<FILE>) { next unless (($good_start) || (/^start-of-data pattern/); $good_start = 1; # do things with the interesting lines }
Re: move down 2 lines in file?
by apl (Monsignor) on Jul 11, 2008 at 14:40 UTC
    Maintain a state flag, and do the extra read once at the end of the loop. For example:
    open (file.....) while (<FILE>) { my $skip_next = 0; if /blah/ { $skip_next = 1; } elsif ( ... something else ... ) { } elsif ( ... third condition ... ) { $skip_next = 1; } .... $_ = <FILE> if $skip_next; } # while reading through the file

    Revised: If you need to skip a variable number of lines, you could set $skip_next to the number of lines you wish to skip, and use a loop rather than the final assignment.

Re: move down 2 lines in file?
by Narveson (Chaplain) on Jul 11, 2008 at 16:19 UTC

    If you know you want to skip a certain number of lines without looking at them:

    my $lines_to_skip = 2 or 3; # or whatever for (1..$lines_to_skip) { <FILE> }
Re: move down 2 lines in file?
by Lawliet (Curate) on Jul 11, 2008 at 15:56 UTC

    Hmm, not sure about this method, but it works in theory.

    open (file.....) while (<FILE>) { $. = 2; # $. holds the number of the last read line .....

    I am not sure if it should be the one I posted above or next if $. == 1 or if $. == 2 though. I have no time to check right now. (Also, I am not sure if it starts counting at 0 or 1. Change the numbers accordingly

    <(^.^-<) <(-^.^<) <(-^.^-)> (>^.^-)> (>-^.^)>
      Changing $. does not move the file pointer. The only way to skip ahead without actually reading would be to use seek() which doesn't mix well with buffered variable-length line-oriented operations.

      --
      [ e d @ h a l l e y . c c ]

        Oh, alright. I thought my idea was pretty cool though~

        Can you (or anyone) confirm if the next if... method works?

        <(^.^-<) <(-^.^<) <(-^.^-)> (>^.^-)> (>-^.^)>
Re: move down 2 lines in file?
by b17m4p (Initiate) on Jul 12, 2008 at 05:27 UTC
    I would just do something like: open FILE, "<", "try.dt" or die $!; while (<FILE>) { next if $. < 2; print $_; } And not think twice about it; is there something wrong with that?
      Sorry new here
      open FILE, "<", "try.dt" or die $!; while (<FILE>) { next if $. <= 2; print $_; }
Re: move down 2 lines in file?
by mvaradhan (Initiate) on Jul 13, 2008 at 14:56 UTC
    open (FILE, "<", "test") or die "can't open" ; sub SKPLN($) {($_[0]!=0) and <FILE> and SKPLN($_[0]-1)} while (<FILE>) { SKPLN(1), next if /blah/ ; SKPLN(2), next if second condition ; ..... }
Re: move down 2 lines in file?
by esteban (Initiate) on Jul 13, 2008 at 07:03 UTC
    Hello, what you do seems right, but could do with some improvement. Consider not asigning the line you want to skip to anything. And if you want to skip a arbitrary number of lines you can use a foreach loop.
    open (file.....) while (<FILE>) { if /blah/ { <FILE>; # Skip one line <FILE> foreach(1..$skip); # Skip a numer of lines

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://696975]
Approved by Corion
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2024-04-24 07:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found