in reply to Re^2: my 'if' condition doesn't work, why?
in thread my 'if' condition doesn't work, why?
A problem with List::Util::first() as used in the example above is that it actually returns the first element from the list that satisfies the condition. If this value happens to be false, the if-statement modifier will not satisfied, and the dependent statement will not be executed. Neither any nor grep nor a hash lookup have this problem.
c:\@Work\Perl>perl -wMstrict -le "use List::Util qw(first); ;; for my $n (0 .. 267) { printf qq{$n } if first {$n == $_} 40, 0, 47, 76; } print qq{\n----------}; ;; ;; for my $n (0 .. 267) { printf qq{$n } if grep { $n == $_ } 40, 0, 47, 76; } " 40 47 76 ---------- 0 40 47 76
|
|---|