Could you please explain what you're trying to accomplish in a little more detail?  
Are you trying to check whether either string is found or only if both are found in 
the same line?

> Folks,
> 
> I have been looking for a way to search for two strings in a line
> at the command line I would
> 
> Communication $grepTMPFILE | grep failure | wc -l
> 
> how would this be converted into perl/regex? I have part of it right now
> (Thanxs to others on the list)
> 
> foreach (@output) {
>   foreach my $test(@lookFor) {
>     $results{$test}++ if /$test/igo;
>   }
> }

This looks like you want to count the number of lines that each element of @lookFor is 
found within @output.  However, the 'o' modifier in your regex will cause perl not to 
update the pattern each time through the loop.  That is, you'll loop through once for 
each element of @lookFor but the regex won't evaluate $test each time through the loop 
(only the first time.)  All elements of %results will have the same value!

> 
> I need to add a && to the if above.. correct?
> 
> if ( $test == 'Communication' ) {
>   if ( /$text/igo && /failed/igo ) { $results($test)++; }
> }

This looks like you're trying to find both strings occurring within the same line 
(assuming you're using the same foreach loops as above), but you'll still have the 
same trouble with the 'o' modifier.


Note that the 'g' modifier isn't necessary if all you want to do is increment the 
count for each line in which the pattern is found.  If you're trying to count all 
occurances of each element of @lookFor ... you'll have to ask the list (I though I 
knew but I just tested my assumption and it was wrong :)

*** Question: Does anyone know how to count the number of times a regex matches?  

      $count = @matches = /$pattern/g;

    This works but seems like a waste.  
    Does anyone know why m//g only returns 1 on success instead of returning the 
number of matches?


Thanks.
-- Brad

Reply via email to