# FAQ 4.42 How can I tell whether a certain element is contained in a list or array?

#### Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

•  Subject
• Author
• Posted on

This message is one of several periodic postings to comp.lang.perl.misc
intended to make it easier for perl programmers to find answers to
common questions. The core of this message represents an excerpt
from the documentation provided with Perl.

--------------------------------------------------------------------

4.42: How can I tell whether a certain element is contained in a list or array?

Hearing the word "in" is an *in*dication that you probably should have
used a hash, not a list or array, to store your data. Hashes are
designed to answer this question quickly and efficiently. Arrays aren't.

That being said, there are several ways to approach this. If you are
going to make this query many times over arbitrary string values, the
fastest way is probably to invert the original array and maintain a hash
whose keys are the first array's values.

@blues = qw/azure cerulean teal turquoise lapis-lazuli/;
%is_blue = ();
for (@blues) { \$is_blue = 1 }

Now you can check whether \$is_blue. It might have been a
good idea to keep the blues all in a hash in the first place.

If the values are all small integers, you could use a simple indexed
array. This kind of an array will take up less space:

@primes = (2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31);
@is_tiny_prime = ();
for (@primes) { \$is_tiny_prime[\$_] = 1 }
# or simply  @istiny_prime[@primes] = (1) x @primes;

Now you check whether \$is_tiny_prime[\$some_number].

If the values in question are integers instead of strings, you can save
quite a lot of space by using bit strings instead:

@articles = ( 1..10, 150..2000, 2017 );
for (@articles) { vec(\$read,\$_,1) = 1 }

Now check whether "vec(\$read,\$n,1)" is true for some \$n.

(\$is_there) = grep \$_ eq \$whatever, @array;

or worse yet

(\$is_there) = grep /\$whatever/, @array;

These are slow (checks every element even if the first matches),
inefficient (same reason), and potentially buggy (what if there are
regex characters in \$whatever?). If you're only testing once, then use:

\$is_there = 0;
foreach \$elt (@array) {
if (\$elt eq \$elt_to_find) {
\$is_there = 1;
last;
}
}
if (\$is_there) { ... }

--------------------------------------------------------------------

Documents such as this have been called "Answers to Frequently
Asked Questions" or FAQ for short.  They represent an important
part of the Usenet tradition.  They serve to reduce the volume of
redundant traffic on a news group by providing quality answers to
questions that keep coming up.

If you are some how irritated by seeing these postings you are free
to ignore them or add the sender to your killfile.  If you find
errors or other problems with these postings please send corrections
or comments to the posting email address or to the maintainers as
directed in the perlfaq manual page.

Note that the FAQ text posted by this server may have been modified
from that distributed in the stable Perl release.  It may have been
edited to reflect the additions, changes and corrections provided
by respondents, reviewers, and critics to previous postings of
these FAQ. Complete text of these FAQ are available on request.

The perlfaq manual page contains the following copyright notice.

Copyright (c) 1997-2002 Tom Christiansen and Nathan
Torkington, and other contributors as noted. All rights
reserved.

This posting is provided in the hope that it will be useful but
does not represent a commitment or contract of any kind on the part
of the contributers, authors or their agents.

## Re: FAQ 4.42 How can I tell whether a certain element is contained in a list or array?

Repair the corrupted file:

perl -p -i -e 'tr/\r//d' bad_file

--
Fort Worth, Texas

## Re: FAQ 4.42 How can I tell whether a certain element is contained in a list or array?

Repair the corrupted file:

perl -p -i -e 'tr/\r//d' bad_file

--