RosettaCodeData/Task/Search-a-list/Perl/search-a-list-3.pl
2023-07-01 13:44:08 -04:00

13 lines
403 B
Perl

my @haystack = qw(Zig Zag Wally Ronald Bush Krusty Charlie Bush Bozo);
my %haystack_indices;
@haystack_indices{ @haystack } = (0 .. $#haystack); # Caution: this finds the largest index, not the smallest
foreach my $needle (qw(Washington Bush)) {
my $index = $haystack_indices{$needle};
if (defined $index) {
print "$index $needle\n";
} else {
print "$needle is not in haystack\n";
}
}