Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Ok, I don't want to beat a dead horse but I've been wondering about this for quite some time now. Suppose you want to see whether or not 'c' is in (a .. z). Of course, I want general case (where the array may contain words instead of letters). What method would you employ to have the program say, "yes, c is a letter that would exist within that array you just created there." Some of the methods I have seen are included in the readmore.

Create a hash and just check for existence (I usually use this):
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"};

Linear search:
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;

Binary search (not complex enough to justify requiring Search::Binary but very similar parameters passed, just in case):
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); } }

Regex hard-coded (seen this used extensively twice before):
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)$/;

Introduce quantum theory?
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);
I am just curious which methods people use for this very common problem and which is the most popular. If there are particular cases where you use one method over another, please cite them and explain why...if you don't mind of course :-)

antirice    
The first rule of Perl club is - use Perl
The
ith rule of Perl club is - follow rule i - 1 for i > 1


In reply to Checking a string's presence within an array. by antirice

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (6)
As of 2024-03-28 21:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found