in reply to Most efficient way of search elements in an array

Use a for loop to cycle through your array elements, and break out of the loop as soon as 0 is found using last:
use strict; use warnings; my @results_file = (1,1,1,0,1); for (@results_file) { if (/0/) { print "\n--FAIL--\n"; last; } else { print "\n--PASS--STATUS\n"; print "\n$_\n"; } } __END__ --PASS--STATUS 1 --PASS--STATUS 1 --PASS--STATUS 1 --FAIL--

Update: Or maybe this is the output you were looking for...

use strict; use warnings; my @results_file = (1,1,1,0,1); my $pass = 1; for (@results_file) { if (/0/) { print "\n--FAIL--\n"; $pass = 0; last; } } if ($pass) { print "\n--PASS--STATUS\n"; print "\n@results_file\n"; } __END__ --FAIL--

Replies are listed 'Best First'.
Re^2: Most efficient way of search elements in an array
by sqspat (Acolyte) on Sep 15, 2009 at 15:16 UTC
    This does it alright ... except I just want one PASS to be printed if the script determines that all the array elements are 1
      Then you want my 2nd example ("Update").

      Also note that my code is equivalent to the pre-5.10 solution offered by Fletch. It wasn't obvious to me from the documentation for List::MoreUtils, until I looked at the source code for any: it does break out of the loop as soon as it finds the first match.