in reply to my 'if' condition doesn't work, why?
This task is almost what grep is designed for:
for my $n (0 .. 267) { print "$n\n" if grep {$n == $_} 40, 47, 76; }
with the slight down side that the compare is evaluated for each item in the list always. Most of the time that won't be an issue, but if it is (for a large list of possibilities) then you could instead:
my %wanted = map {$_ => 1} 40, 47, 76; for my $n (0 .. 267) { print "$n\n" if exists $wanted{$n}; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: my 'if' condition doesn't work, why?
by eyepopslikeamosquito (Archbishop) on Nov 12, 2014 at 07:45 UTC | |
by AnomalousMonk (Archbishop) on Nov 12, 2014 at 12:10 UTC | |
by GrandFather (Saint) on Nov 12, 2014 at 08:03 UTC |