perldoc -q element
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{$some_color}. It might have been a good
idea to keep the blues all in a hash in the first place.
Update: misread the question, sorry
|