#!/usr/bin/perl -wT use strict; my @arr = 1..10; my $tofind = 5; my $found; { grep {$_ == $tofind and $found = 1 and last} @arr; # wont get here if element is found; } print $found ? "I found" : "I didn't find", " element $tofind\n";
Update: changed eq to ==. Thanks to dvergin for the catch.
Update2: managed to munge my previous update... thanks to tomhukins for this catch.
-Blake
In reply to Re: Quickly finding an element in an array
by blakem
in thread Quickly finding an element in an array
by tomhukins
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |