RosettaCodeData/Task/Search-a-list/Perl/search-a-list-1.pl
Ingy döt Net 86c034bb8b new files
2013-04-10 12:38:42 -07:00

13 lines
483 B
Perl

use List::Util qw(first);
my @haystack = qw(Zig Zag Wally Ronald Bush Krusty Charlie Bush Bozo);
foreach my $needle (qw(Washington Bush)) {
my $index = first { $haystack[$_] eq $needle } (0 .. $#haystack); # note that "eq" was used because we are comparing strings
# you would use "==" for numbers
if (defined $index) {
print "$index $needle\n";
} else {
print "$needle is not in haystack\n";
}
}