PrincessRTFM
PrincessRTFM

Reputation: 179

Check whether an array contains a value from another array

I have an array of objects, and an array of acceptable return values for a particular method. How do I reduce the array of objects to only those whose method in question returns a value in my array of acceptable values?

Right now, I have this:

my @allowed = grep {
    my $object = $_;
    my $returned = $object->method;
    grep {
        my $value = $_;
        $value eq $returned;
    } @acceptableValues;
} @objects;

The problem is that this is a compound loop, which I'd like to avoid. This program is meant to scale to arbitrary sizes, and I want to minimize the number of iterations that are run.

What's the best way to do this?

Upvotes: 6

Views: 2266

Answers (2)

Kamal Nayan
Kamal Nayan

Reputation: 1940

Elements supposed to be present in given arrays seems unique. So, I will make a hash containing the count of elements from both arrays. If there is any element with count greater than 1, it means its present in both the arrays.

my %values;
my @allowed;
map {$values{$_}++} (@acceptableValues, @objects);
for (keys %values) {
    push @allowed, $_ if $values{$_} > 1;
}

Upvotes: 1

sidyll
sidyll

Reputation: 59277

You could transform the accepted return values into a hash

my %values = map { $_ => 1 } @acceptedValues;

And grep with the condition that the key exists instead of your original grep:

my @allowed = grep $values{ $_->method }, @objects;

Anyway, grep is pretty fast in itself, and this is just an idea of a common approach to checking if an element is in an array. Try not to optimize what's not needed, since it would only be worth in really big arrays. Then you could for example sort the accepted results array and use a binary search, or cache results if they repeat. But again, don't worry with this kind of optimisation unless you're dealing with hundreds of thousands of items — or more.

Upvotes: 7

Related Questions