new files
This commit is contained in:
parent
3af7344581
commit
86c034bb8b
1364 changed files with 21352 additions and 0 deletions
13
Task/Search-a-list/Perl/search-a-list-1.pl
Normal file
13
Task/Search-a-list/Perl/search-a-list-1.pl
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
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";
|
||||
}
|
||||
}
|
||||
13
Task/Search-a-list/Perl/search-a-list-2.pl
Normal file
13
Task/Search-a-list/Perl/search-a-list-2.pl
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
use List::MoreUtils qw(first_index);
|
||||
|
||||
my @haystack = qw(Zig Zag Wally Ronald Bush Krusty Charlie Bush Bozo);
|
||||
|
||||
foreach my $needle (qw(Washington Bush)) {
|
||||
my $index = first_index { $_ eq $needle } @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";
|
||||
}
|
||||
}
|
||||
13
Task/Search-a-list/Perl/search-a-list-3.pl
Normal file
13
Task/Search-a-list/Perl/search-a-list-3.pl
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
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";
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue