in reply to check if an element exists in array
For those getting here through the Google terms "element exists in array perl", here's a simple routine I wrote because I wanted something similar to TCL's "lsearch -exact" but with a binary return:
Of course, this should only be used on small haystacks (a few hundred elements at most).# Checks if a provided element exists in the provided list # Usage: isInList <needle element> <haystack list> # Returns: 0/1 sub isInList { my $needle = shift; my @haystack = @_; foreach $hay (@haystack) { if ( $needle eq $hay ) { return 1; } } return 0; }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: check if an element exists in array
by jdporter (Paladin) on Apr 12, 2011 at 16:49 UTC | |
by RecursionBane (Beadle) on Apr 13, 2011 at 18:13 UTC | |
by jdporter (Paladin) on Apr 13, 2011 at 18:42 UTC |
In Section
Seekers of Perl Wisdom