Jim Gibson <jimsgib...@gmail.com> writes:

[...]

> You don't need to compare regular expressions. If you did, then you would
> use the 'eq' operator on the strings that generate the regular expressions.
> However, that is unreliable, as two strings can generate the same effective
> pattern, as, for example, 'a?' is equivalent to 'a{0,1}'.
>
> You should use the OR logical operator on the results from matching the
> input string against the two regular expressions:
>
>     if( $line =~ /$date_re/ || $line =~ /$other_re/ ) {
>         print $line; # or collect for later use
>     }
>
> Or, if there are many more possible regular expressions to try, set a flag
> for each input line if any pattern matches, and use the value of the flag to
> determine further action on that line.

I think I might have hit a more tidy way than what I posted and it
follows your suggestions too:
   while (<FILE>){
     [ blab other operations]

      if(/^$/){# if we see a blankline
        if(@InFileHits){ ## test if @InFileHits has value
         if($dateline){ # Print it if we have it
           print "$dateline]n";
         }
         [... other print operations here ]
        }
        last; # leave while loop and back to find()
      }

Now the match is against the line ($_)
      if (/$date_re/ && ! /$InFileRe/) {#grab this line
        $dateline = $_;
        next;
      }
Only scoops up dateline if InFileRe DOESN't match it.
Cuts out lots of if/else baloney I posted.

      if(!/$InFileRe/){
        next;
      }
      if(/$InFileRe/){## grab this line
        push @InFileHits, $_;
        next;
      }        
  }


-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to