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

Perl Gurus: I want to do a simple count on a page if it meets to criteria. The problem is when skip two lines to check if it meets the first if statement.
while(<SOURCE>) { my $source = $_; next if ($source =~ m/^(\s)*$/); next if ($source =~ m/^(\s)*$/); #Go through each line until you hit page 1 of sorter 2 if (substr($source,145,16) =~ /Page: 1/) { next; next; if (substr($source,72,9) =~ /Sorter: 2/) { ++$dcount; printf LOGFILE $source; } } }
Please let me know if anyone can help. Dave

Replies are listed 'Best First'.
Re: Need to skip two lines
by bichonfrise74 (Vicar) on May 05, 2009 at 22:47 UTC
    Will this solve your problem?
    #!/usr/bin/perl use strict; my $skip_count = 0; while (<DATA>) { if ( $skip_count > 0 ) { $skip_count--; next; } $skip_count = 2 if ( /^abc/ ); print; } __DATA__ def abc skip_1 skip_2 xyz
      This is how you skip lines without affecting the rest of the outer while loop.
      while(<DATA>){ if (/2/){ my $skip=2; print "skipping $skip lines\n"; <DATA> while $skip-- >0 and not eof DATA; next; } if (/5/){ print "high five\n"; next; } if (/3/){ print "I am not here\n"; next; } print; } __DATA__ 1 1 2 3 4 4 5

      print+qq(\L@{[ref\&@]}@{['@'x7^'!#2/"!4']});
      I think that might work. Let me put that in my script to see what happens. Thanks, Dave
Re: Need to skip two lines
by Marshall (Canon) on May 05, 2009 at 23:59 UTC
    First, if you have a next statement that evaluates to True, there is "no next after that next"! Next in Perl is like "continue" in other languages.

    This code doesn't make sense:

    next if ($source =~ m/^(\s)*$/); next if ($source =~ m/^(\s)*$/); #redundant!
    There are 5 white space characters: \s,\n,\t,\r,\f. So if you have a program that generates new pages, just count the number of \f's!
    There is obviously some point that I am missing here! substr() is highly unlikely to produce usable results. Better probably is a REGEX that counts the number of pages or use \f for that.
Re: Need to skip two lines
by spx2 (Deacon) on May 06, 2009 at 02:09 UTC
    drodinthe559 you seem to think that next will not alter the control flow of your program , which in this case it does alter. In other words , you cannot write two consecutive next statements and hope that you will skip 2 iterations of the while loop. However , you can skip 2 iterations like this
    while(<SOURCE>) { my $source = $_; if($source =~ m/^(\s)*$/) { $source = <SOURCE> for 1..2; } #Go through each line until you hit page 1 of sorter 2 if (substr($source,145,16) =~ <b>/Page:\s*1/)</b> { $source = <SOURCE> for 1..2; if (substr($source,72,9) =~ /Sorter: 2/) { ++$dcount; printf LOGFILE $source; } } }
Re: Need to skip two lines
by citromatik (Curate) on May 06, 2009 at 07:34 UTC
    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

      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? }