in reply to The most efficient way for searching for an element in an array?

Hello Ppeoc,

If efficiency is really important, you should use a short-circuiting function rather than grep, which evaluates all instances in the input list. For example, from List::Util#any:

Many cases of using grep in a conditional can be written using any instead, as it can short-circuit after the first true result.

BTW, are you certain that @Single and @Double will never have values in common? If not, you may need to change the logic to incorporate a fourth flag:

use strict; use warnings; use Data::Dump; use List::Util qw( any ); my @choices = qw/Apple Banana Tiger Lion Cat Turtle/; my @Single = qw/Apple Turtle/; my @Double = qw/Apple Lion/; my %choices; for my $field (@choices) { my $flag = 0; $flag = 1 if any { $_ eq $field } @Single; $flag += 2 if any { $_ eq $field } @Double; $choices{$field} = $flag; } # $flag == 3 means $field is present in both @Single and @Double dd \%choices;

Output:

14:27 >perl 1508_SoPW.pl { Apple => 3, Banana => 0, Cat => 0, Lion => 2, Tiger => 0, Turtle => +1 } 14:28 >

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,