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

I'm stuck in stupid logic land. I'm stepping through a log file and when the first indicator is found I want to print $_ until the next indicator is located. Here's what I'm trying to do.....
sub BFPRCONS(){ $BFPRCONSCount = $BFPRCONSCount + 1; $BFPRCONSFileName = $job."_".$orgCycleNumber."_".$junk1."_".$filed +ate."_".$junk2; open (DATA, "<c:/Logs/FBF/$BFPRCONSFileName") or die "Couldn't ope +n, $!"; @lines = <DATA>; close (DATA); foreach (@lines){ chomp; $bfprconsindicator = substr($_, 0, 44); if ($bfprconsindicator eq "========== Summary of bfpr_catfl result +s ==="){ print "$_\n"; } } }
Please help brain-locked. Thanks

Replies are listed 'Best First'.
Re: Stupid parsing question
by Fastolfe (Vicar) on Nov 27, 2001 at 21:28 UTC
    For the benefit of others that may want a solution to the problem you initially state, consider using the bi-stable/flip-flop operator:
    while ($looping) { do_something if condition_one .. condition_two; }
    Example:
    while (<DATA>) { print if /two/ .. /four/; } __DATA__ line one line two line three line four line five
    This would print out lines 2, 3 and 4.

    See perlop.

      This is really cool! That snippet just made my
      visit worthwhile for the entire week!
(jeffa) Re: Stupid parsing question
by jeffa (Bishop) on Nov 27, 2001 at 21:06 UTC
    You are not giving enough info - what does the 'next indicator' look like? I will go ahead and assume one.

    Pointers - pass args to subroutines, don't rely on globals. Second, don't name a filehandle DATA, because there is already a built-in DATA filehandle. Third, use a regex on a scalar that contains the file contents, not a for loop on an array.

    Check this out:

    use strict; my $contents = do {local $/; <DATA>}; print get_chunk($contents); sub get_chunk { my ($contents) = @_; my $start = qr|========= Summary of bfpr_catfl results ===\s*|; my $end = qr|========= end ===\s*|; my ($chunk) = $contents =~ /$start(.*)$end/s; return $chunk; } __DATA__ you won't see this or this ========= Summary of bfpr_catfl results === you will see this and this ========= end === you won't see this or this or that
    See perlre for more info on regexes and the 's' option and perlop for info on 'qr'.

    UPDATE:
    I notice you tend to group variables together with a common prefix - BFPRCONS - you should try a global hash (not all globals are bad):

    my %BFPRCONS = ( filename => 'whatever_the_name_is', count => 0, start => qr|===== summary blah ===|, end => qr|===== end blah ===|, ); # then you could inc the count like so: $BFPRCONS{'count'}++; # you could set the file name like so: $BFPRCONS{'filename'} = join ('_',$job,$junk1,$etc);

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    F--F--F--F--F--F--F--F--
    (the triplet paradiddle)
    
      Thanks, that did it. Really appreciate the help. I hate duh-land.
Re: Stupid parsing question
by Fletch (Bishop) on Nov 27, 2001 at 20:57 UTC
    my $printing = 0; while( <DATA> ) { if( /indicator/ ) { $printing = !$printing; next; # presuming you don't want to print the indicator itself } print if $printing; } __DATA__ foo bar indicator baz quux fnord indicator flig? boink
      This is gonna work in another place. Thanks a lot.