Thanks for all the replies. Yes I found List::Util is a useful toolset.
On 2016/8/19 10:00, Chas. Owens wrote:
The any function from List::Util will also do what you want. perldoc List::Util http://perldoc.perl.org/List/Util.html#any my $found = any { $_ == 4 } (3, 1, 4, 2, 9, 0); # true my $not_found = any { $_ == 10 } (3, 1, 4, 2, 9, 0); # false Which you want depends on the application. The grep function will return a number between 0 and the size of the list and reads the entire list. The any function returns the canonical true (a tri-value that holds "1", 1, and 1.0) or false (a tri-value that holds "", 0, 0.0) values and stops at the first matching value. The canonical false value often throws people for a loop as they expect it to be "0" in string context, but it is "". You may want to say my $found = (any { $_ == 10 } (3, 1, 4, 2, 9, 0)) || 0; to force it to be 0 instead of the canonical false value.
-- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/