antirice has asked for the wisdom of the Perl Monks concerning the following question:
my @array_I_just_created_here = (a .. z); my @hash_I_just_created_here{@array_I_just_created_here} = (); print "yes, c is a letter that would exist within that array you just +created there.$/" if exists $hash_I_just_created_here{"c"};
my $found = 0; my @array_I_just_created_here = (a .. z); $found |= ("c" eq $array_I_just_created_here[$_]) foreach (0..$#array_ +I_just_created_here); print "yes, c is a letter that would exist within that array you just +created there.$/" if $found;
my $find_item = "c"; my @array_I_just_created_here = (a .. z); my $pos = bin_search(\@array_I_just_created_here, $find_item, 0, $#arr +ay_I_just_created_here); print "yes, c is a letter that would exist within that array you just +created there.$/" if ($a[$pos] eq $find_item); sub bin_search { my ($array, $val, $min, $max) = @_; my $pos = int($min + $max / 2); my $check = $val cmp $array->[$pos]; if ($check == -1) { $pos--; return $pos if ($pos <= $min); return bin_search($array, $val, $min, $pos); } elsif ($check == 0) { return $pos; } else { $pos++; return $pos if ($pos >= $max); return bin_search($array, $val, $pos, $max); } }
print "yes, c is a letter that would exist within that array you hadn' +t created there.$/" if "c" =~ m/^(?:a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q +|r|s|t|u|v|w|x|y|z)$/;
use Quantum::Superpositions; my @array_I_just_created_here = (a .. z); print "yes, c is a letter that would exist within that array you just +created there.$/" if ("c" eq any(@array_I_just_created_here);
antirice
The first rule of Perl club is - use Perl
The ith rule of Perl club is - follow rule i - 1 for i > 1
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Checking a string's presence within an array.
by pzbagel (Chaplain) on May 16, 2003 at 18:20 UTC | |
Re: Checking a string's presence within an array.
by broquaint (Abbot) on May 16, 2003 at 18:47 UTC | |
Re: Checking a string's presence within an array.
by hardburn (Abbot) on May 16, 2003 at 18:11 UTC |