21

I have a list of possible values:

@a = qw(foo bar baz);

How do I check in a concise way that a value $val is present or absent in @a?

An obvious implementation is to loop over the list, but I am sure TMTOWTDI.


Thanks to all who answered! The three answers I would like to highlight are:

  1. The accepted answer - the most "built-in" and backward-compatible way.

  2. RET's answer is the cleanest, but only good for Perl 5.10 and later.

  3. draegtun's answer is (possibly) a bit faster, but requires using an additional module. I do not like adding dependencies if I can avoid them, and in this case do not need the performance difference, but if you have a 1,000,000-element list you might want to give this answer a try.

3
  • 1
    I'm not sure that I see the dependency issue with List::Util. It's standard with Perl and if you use it with qw/first/ (as Draegtun did), you import only the one subroutine.
    – Telemachus
    Apr 6, 2009 at 12:14
  • It's not an issue per se, it's more of a personal preference.
    – MaxVT
    Apr 6, 2009 at 13:14
  • 1
    There is no dependency issue with the List::Util answer. If it was me, that would be the accepted answer. Unwillinglness to use core modules strikes me as a preference rooted in superstition. In this case grep {} is almost as good though. Apr 7, 2009 at 6:14

8 Answers 8

40

If you have perl 5.10, use the smart-match operator ~~

print "Exist\n" if $var ~~ @array;

It's almost magic.

2
20

Perl's bulit in grep() function is designed to do this.

@matches = grep( /^MyItem$/, @someArray ); 

or you can insert any expression into the matcher

@matches = grep( $_ == $val, @a ); 
0
18

This is answered in perlfaq4's answer to "How can I tell whether a certain element is contained in a list or array?".

To search the perlfaq, you could search through the list of all questions in perlfaq using your favorite browser.

From the command line, you can use the -q switch to perldoc to search for keywords. You would have found your answer by searching for "list":

perldoc -q list

(portions of this answer contributed by Anno Siegel and brian d foy)

Hearing the word "in" is an indication 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. In Perl 5.10 and later, you can use the smart match operator to check that an item is contained in an array or a hash:

use 5.010;

if( $item ~~ @array )
    {
    say "The array contains $item"
    }

if( $item ~~ %hash )
    {
    say "The hash contains $item"
    }

With earlier versions of Perl, you have to do a bit more work. 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.

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 );
undef $read;
for (@articles) { vec($read,$_,1) = 1 }

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

These methods guarantee fast individual tests but require a re-organization of the original list or array. They only pay off if you have to test multiple values against the same array.

If you are testing only once, the standard module List::Util exports the function first for this purpose. It works by stopping once it finds the element. It's written in C for speed, and its Perl equivalent looks like this subroutine:

sub first (&@) {
    my $code = shift;
    foreach (@_) {
        return $_ if &{$code}();
    }
    undef;
}

If speed is of little concern, the common idiom uses grep in scalar context (which returns the number of items that passed its condition) to traverse the entire list. This does have the benefit of telling you how many matches it found, though.

my $is_there = grep $_ eq $whatever, @array;

If you want to actually extract the matching elements, simply use grep in list context.

my @matches = grep $_ eq $whatever, @array;
16

Use the first function from List::Util which comes as standard with Perl....

use List::Util qw/first/;

my @a = qw(foo bar baz);
if ( first { $_ eq 'bar' } @a ) { say "Found bar!" }

NB. first returns the first element it finds and so doesn't have to iterate through the complete list (which is what grep will do).

2
  • 1
    per foolishbrat, if using an imported sub, I would prefer List::MoreUtil::any() because the concept ("Returns a true value if any item in LIST meets the criterion") is semantically a better match to the question than first() ("returns the first element where the result from BLOCK is a true value.")
    – nohat
    Apr 7, 2009 at 1:19
  • List::Util::first() has the advantage of being core module (ie. ubiquitous). If I were looking for CPAN options then I would seriously consider Perl6::Junction::any... if ( any(@a) eq 'baz' ) {}
    – draegtun
    Apr 7, 2009 at 9:56
8

One possible approach is to use List::MoreUtils 'any' function.

use List::MoreUtils qw/any/;

my @array = qw(foo bar baz);

print "Exist\n" if any {($_ eq "foo")} @array;

Update: corrected based on zoul's comment.

2
  • Defined("foo") is always true, did You mean $_ eq 'foo'?
    – zoul
    Apr 6, 2009 at 7:40
  • I keep taking off the -1 I gave you and it keeps putting it back on... Hope it stays off this (3rd) time! Apr 8, 2009 at 7:31
5

Interesting solution, especially for repeated searching:

my %hash;
map { $hash{$_}++ } @a;
print $hash{$val};
2
  • Zoul's suggestion about a hash is pretty close to optimal however I would suggest that as you add and remove values to your array during the course of your program add and remove those value in your hash. Apr 6, 2009 at 8:25
  • Also, although this works and is common, some people (myself included I guess) will complain about using map in a void context. Why not $hash{$_}++ for @a instead?
    – jettero
    Apr 6, 2009 at 10:13
2
$ perl -e '@a = qw(foo bar baz);$val="bar";
if (grep{$_ eq $val} @a) {
  print "found"
} else {
  print "not found"
}'

found

$val='baq';

not found

1

If you don't like unnecessary dependency, implement any or first yourself

sub first (&@) {
  my $code = shift;
  $code->() and return $_ foreach @_;
  undef
}

sub any (&@) {
  my $code = shift;
  $code->() and return 1 foreach @_;
  undef
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.