in reply to Re: searching through array for multiple patterns
in thread searching through array for multiple patterns

Hi Presently the code searches through each line looking for the pattern and prints success statement if it finds in a line and failure statement if it does not find it (for each line). I want it to either print success or failure statement once for the searching of the whole array. thanks
  • Comment on Re^2: searching through array for multiple patterns

Replies are listed 'Best First'.
Re^3: searching through array for multiple patterns
by cdarke (Prior) on Jul 16, 2009 at 11:14 UTC
    I'm with you, in that case just set a flag if the element is found, then break out of the loop at once (no point in continuing to search):
    my $found = 0; foreach (@commands_run){ if($_ =~ m/clock/) { $found = 1; last; } } if ($found) { print "\n Test -- found pattern -- \n" } else { print "\n Test -- did not find pattern -- \n" }
Re^3: searching through array for multiple patterns
by rovf (Priest) on Jul 16, 2009 at 08:17 UTC

    maybe somthing like

    if(index("@commands_run",'clock') >= 0) { # string 'clock' found somewhere in array }

    -- 
    Ronald Fischer <ynnor@mm.st>