Faster way to check for element in array?

Viewed 4895

This function does the same as exists does with hashes.

I plan on use it a lot.

Can it be optimized in some way?

my @a = qw/a b c d/;

my $ret = array_exists("b", @a);

sub array_exists {
    my ($var, @a) = @_;

    foreach my $e (@a) {
        if ($var eq $e) {
            return 1;
        }
    }
    return 0;
}
7 Answers
Related