Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Purpose
On a regular basis, people want to know if a particular thing, we will call it foo, is present in an array. The problem with just directing them to perldoc -q contain is that each question usually varies enough to make a single answer unsatisfactory. The goal of this tutorial is to give enough information so that someone can choose the right solution to the problem.

Is This Tutorial For You?
A typical mistake is to assume that there is a built-in way of determining presence in an array like there is with a hash. A red flag should go up if you try using a string for an index as in print "gotcha\n" if $array['foo']. Perhaps you should be using a hash. An array gives no "meaning" between the index and the value (though the programmer might as in the case of matrices). If you are still unsure which to be using, see perldoc perldsc and perldoc perldata for more information.

What Will Be Covered
There are typically 5 answers to the problem of identifying if foo is present in an array.

  • Convert the array to a hash
  • Use the built-in grep
  • Use List::Util's first
  • Use the smart match operator ~~ introduced in 5.10.0
  • IYAW (Invent Yet Another Wheel)
There are a handful of things to consider and each solution has pros and cons accordingly. You will need to decide for yourself which is best.

Convert The Array To A Hash
Depending on your requirements, this can be a very attractive solution (print "It's in there\n" if exists $sauce{spaghetti}). There is a list of things to consider:

  • How many items do you need to check?
  • If you are only checking a single item, this is probably not the best solution.
  • Will the array change between checks?
  • If you need to rebuild the hash each time you check because you are not sure if the array has changed, this is probably not the best solution.
  • Do you need something other than an exact match?
  • Using a hash is a bad idea for non exact matches. The one noteable exception is perhaps case insensitivity. You can force the keys to a certain case during creation and foo to the same case when searching. Even then, it is questionable with regards to this being the best solution.
  • How big is the array?
  • Duplicating data structures for the convenience of searching can get quite expensive with regards to memory consumption. You will need to determine how large a footprint is acceptable to you.
  • Do you need to know if there is more than one match?
  • Remember that hash keys must be unique so using a hash when there is a possibility of duplicates is a bad idea if you need to know how many times foo is present in the array.
  • Do you need to know foo's index?
  • Typically for memory reasons, you would build your hash like:
    my %lookup = map { $_ => undef } @array; print "There it is\n" if exists $lookup{foo};
    But if you have given some meaning to the relationship to the index and the value (or perhaps you want to use splice to remove it), then you should consider using the following instead:
    my %lookup = map { $array[$_] => $_ } 0 .. $#array; my $index = $lookup{foo};

Use The Built-In grep
It is fast and powerful and deserves careful consideration:

  • How many items do you need to check?
  • If you are checking many items, this is probably not the best solution because it goes through the entire list even if the match is found the first time. Keep in mind that many is subjective and since it has been optimized for efficiency, it still may be acceptable.
  • Will the array change between checks?
  • You might want to use grep if the answer is yes or you are unsure since it is not checking against a copy that might be stale.
  • Do you need something other than an exact match?
  • You might want to use grep if the answer is yes or you are unsure. Since grep can accept a block, you can easily do a case insensitive search (grep { $_ =~ /^foo$/i } @array)
  • How big is the array?
  • If the array is very large, you may want to consider something other than grep. Of course very large is subjective. If you are not going to be checking numerous items and you can afford to wait a few extra seconds, grep might be the perfect choice.
  • Do you need to know if there is more than one match?
  • Since grep goes through the entire list, it is an ideal candidate if the answer to this question is yes. In list context, grep will return the list of matching items. In scalar context - it will return the number of matching items.
  • Do you need to know foo's index?
  • Since grep can take a block, it is very versatile and can easily be adapted to this situation:
    my @indices = grep { $array[$_] eq 'foo' } 0 .. $#array;

Use List::Util's first
It works very much like grep except it returns on the first match, which depending on your situation can make it a perfect solution. It is worth noting that if it does not find a match, it returns undef which may be problematic if that's the value you are trying to match.

  • How many items do you need to check?
  • At first glance, you might say it does not matter. One issue is that the first function is not a compiled built-in like grep. A code block is dereferenced and executed for each element in the list until a match is found. A second issue is that first does not currently accept a reference as an argument so a list of aliases has to be generated. Be sure not to worry about premature optimization when the net difference in runtime is a few seconds unless it really matters.
  • Will the array change between checks?
  • Like grep, if the answer is yes or you are unsure, you should be considering first since it is not checking against a copy that might be stale.
  • Do you need something other than an exact match?
  • Like grep, if the answer is yes or you are unsure then first is a possibility since it takes a block.
  • How big is the array?
  • Again, you might say it doesn't matter at first glance since it returns on first match. The first function was built to be flexible accepting a code block that has to be dereferenced against each item in the list until a match is found. If you really need lightening speed then you might want to consider a roll-your-own version that is hard-coded.
  • Do you need to know if there is more than one match?
  • If the answer is yes, do not choose first since it is designed to return on first match.
  • Do you need to know foo's index?
  • The first function can be adapted to handle this situation just like grep.

Use the smart match operator ~~ introduced in perl 5.10.0
If all you need to know is if an item is in the array or not, this may be the fastest solution. The smart match operator can do much more complicated matching, but since 5.10.0 is only a few days old this tutorial does not cover them.

  • Do you need to maintain backwards compatibility?
  • If the answer is yes, the smart match operator is not the best option.
  • Do you need anything more complicated than an exact match?
  • The smart match operator can probably handle it but you will need to Benchmark to determine if it is the best solution. See the latest perlsyn for details.

IYAW (Invent Yet Another Wheel)
There is rarely a good reason to do this. Sanity checks such as profiling and benchmarking should be done before you spending valuable time trying to squeeze a couple of seconds off the runtime. I can only think of a couple of reasons you might want to do this:

  • You can't afford the performance hit of alias construction and repeatedly dereferencing the code block passed to first
  • You need to do some transformation before checking but need to keep the original array intact
  • I freely admit that this falls out of the scope of checking the presence of an item in an array. By copying the array and then transforming it you have changed the problem. This does not stop people from asking and so I have included it here for an attempt at completeness.

Update: I modified the code snippet in the "Is This Tutorial For You" section to remove exists per rob_au's suggestion. It was not necessary to illustrate the point.

Update 2: 2004-10-02 - wolv pointed out to me that the inefficiencies in List::Util's first are in the pure perl version. The XS version is obviously much faster. Benchmark to be sure but it still loses to grep in many situations.

Update 3: 2007-12-22 - Added smart match operator section as a result of the release of perl 5.10.0. See this for a speed comparison.


In reply to Getting Matching Items From An Array by Limbic~Region

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 admiring the Monastery: (3)
As of 2024-03-29 06:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found