"Dr.Ruud" <rvtol+use...@isolution.nl> writes: > Harry Putnam wrote: > >> my $date_re = qr/^Date:/; >> my $other_re = qr/^Date: some date/; >> >> if (/$date_re/ !~ /$other_re/){ > > That code is bizar! > > What exactly do you want to compare? > 1. Whether both matches succeed in the same way? > 2. Whether both regexps contain similar code? > 3. none of the above
I guess 2 is closest, but Jim got me lined out a bit on that. The snippet is simplified and from a larger script that searches mail/news headers for a regex given on the command line. It returns the line containing the regex plus the line containing ^Date: and the absolute filename of the file where match occurred. Then when a blank line shows up, if a hit has occurred it prints the desired output, then lasts out of while loop and goes on to the next file in a find() function. What that code attempted to do was prevent the script from returning two Date lines, in the event the regex given on command line was also a Date regex. As Jim G, said its never necessary to match two regex against each other, and especially when perl is expecting the match to be aimed at $_. On my possibly wrong understanding of Jim G's advice I fixed the `bizarre' mess by making the match aim at $_ like this: if(/$date_re/ && ! /$other_re){ $dateline = $_; } In that case both possible matches are aimed at $_ and collecting two Datelines is prevented. -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/