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.

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:

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

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.

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.

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:

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.