in reply to Quickly finding an element in an array
#!/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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Quickly finding an element in an array
by tomhukins (Curate) on Jan 08, 2002 at 01:48 UTC | |
by blakem (Monsignor) on Jan 08, 2002 at 01:56 UTC |