Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Need to skip two lines

by citromatik (Curate)
on May 06, 2009 at 07:34 UTC ( #762183=note: print w/ replies, xml ) Need Help??


in reply to Need to skip two lines

while (<DATA>){ <DATA> && <DATA> if (/3/); print; } __DATA__ 1 2 3 skipme skipme 4 5

Or in the general case:

do {<DATA> for (0..$x)} if (/3/);

where $x is the number of lines you want to skip -1

citromatik


Comment on Re: Need to skip two lines
Select or Download Code
Re^2: Need to skip two lines
by drodinthe559 (Monk) on May 06, 2009 at 20:11 UTC
    Here is the script I finally came up with. It seems to work; although, I would like your opinion on any improvements I should make.
    while(<SOURCE>) { my $source = $_; #skip blank lines next if ($source =~ m/^(\s)*$/); if (substr($source,145,16) =~ /Page: 1/) { $page1 = 1; next; } if (substr($source,76,11) =~ /Run Listing/) { $reporttype = 1; next; } if (substr($source,72,9) =~ /Sorter: 2/) { $sorter2 = 1; if ($page1 == 1 and $reporttype == 1 and $sorter2 == 1){ ++$dcount; print substr($source,83,20) . "\n"; $page1 = 0; $reporttype = 0; $sorter2 = 0; next; } }

      You could consider the following things:

      • Don't use $_ as loop variable; use $source directly
      • remove the parentheses in the regex (for skipping blank lines); you don't need to capture the whitespaces, do you?
      • compare fixed strings directly (without regex); check Equality Operators for info about eq and ne
      • I don't see a need for $sorter2; just leave it out.
      • If all your ifs are alternatives, you can use if/elsif/elsif(/else) and leave out the next calls.

      So this is, what I came up with:

      # don't bother $_; use $source directly as loop variable while ( my $source = <SOURCE> ) { # skip blank lines; no need for capturing () next if $source =~ m/^\s*$/; # compare fixed strings directly without regex; use operators eq o +r ne if ( substr( $source, 145, 16 ) eq 'Page: 1' ) { $page1 = 1; } elsif ( substr( $source, 76, 11 ) eq 'Run Listing' ) { $reporttype = 1; } elsif ( substr( $source, 72, 9 ) eq 'Sorter: 2' ) { # no need for $sorter2; just check the other variables and do +your stuff if ( $page1 == 1 and $reporttype == 1 ) { # just personal preference; post increment $dcount++; # personal preference; print a list instead of concatted s +trings print substr( $source, 83, 20 ), "\n"; $page1 = 0; $reporttype = 0; # leave out next, if there is no more code in the while-lo +op #next; } } # no other stuff here? }

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://762183]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (19)
As of 2014-07-28 19:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (207 votes), past polls