Perl: if ( element in list )
I'm looking for presence of an element in a list.
In Python there is an in
keyword and I would do something like:
if element in list:
doTask
Is there something equivalent in Perl without having to manuall开发者_运维问答y iterate through the entire list?
UPDATE:
The smartmatch family of features are now experimental
Smart match, added in v5.10.0 and significantly revised in v5.10.1, has been a regular point of complaint. Although there are a number of ways in which it is useful, it has also proven problematic and confusing for both users and implementors of Perl. There have been a number of proposals on how to best address the problem. It is clear that smartmatch is almost certainly either going to change or go away in the future. Relying on its current behavior is not recommended.
Warnings will now be issued when the parser sees ~~, given, or when.
If you can get away with requiring Perl v5.10, then you can use any of the following examples.
The smart match
~~
operator.if( $element ~~ @list ){ ... } if( $element ~~ [ 1, 2, 3 ] ){ ... }
You could also use the
given
/when
construct. Which uses the smart match functionality internally.given( $element ){ when( @list ){ ... } }
You can also use a
for
loop as a "topicalizer" ( meaning it sets$_
).for( @elements ){ when( @list ){ ... } }
One thing that will come out in Perl 5.12 is the ability to use the post-fix version of when
. Which makes it even more like if
and unless
.
given( $element ){
... when @list;
}
If you have to be able to run on older versions of Perl, there still are several options.
You might think you can get away with using List::Util::first, but there are some edge conditions that make it problematic.
In this example it is fairly obvious that we want to successfully match against
0
. Unfortunately this code will printfailure
every time.use List::Util qw'first'; my $element = 0; if( first { $element eq $_ } 0..9 ){ print "success\n"; } else { print "failure\n"; }
You could check the return value of
first
for defined-ness, but that will fail if we actually want a match againstundef
to succeed.You can safely use
grep
however.if( grep { $element eq $_ } 0..9 ){ ... }
This is safe because
grep
gets called in a scalar context. Arrays return the number of elements when called in scalar context. So this will continue to work even if we try to match againstundef
.You could use an enclosing
for
loop. Just make sure you calllast
, to exit out of the loop on a successful match. Otherwise you might end up running your code more than once.for( @array ){ if( $element eq $_ ){ ... last; } }
You could put the
for
loop inside the condition of theif
statement ...if( do{ my $match = 0; for( @list ){ if( $element eq $_ ){ $match = 1; last; } } $match; # the return value of the do block } ){ ... }
... but it might be more clear to put the
for
loop before theif
statement.my $match = 0; for( @list ){ if( $_ eq $element ){ $match = 1; last; } } if( $match ){ ... }
If you're only matching against strings, you could also use a hash. This can speed up your program if
@list
is large and, you are going to match against%hash
several times. Especially if@array
doesn't change, because then you only have to load up%hash
once.my %hash = map { $_, 1 } @array; if( $hash{ $element } ){ ... }
You could also make your own subroutine. This is one of the cases where it is useful to use prototypes.
sub in(&@){ local $_; my $code = shift; for( @_ ){ # sets $_ if( $code->() ){ return 1; } } return 0; } if( in { $element eq $_ } @list ){ ... }
if( $element ~~ @list ){
do_task
}
~~
is the "smart match operator", and does more than just list membership detection.
grep
is helpful here
if (grep { $_ eq $element } @list) {
....
}
If you plan to do this many times, you can trade-off space for lookup time:
#!/usr/bin/perl
use strict; use warnings;
my @array = qw( one ten twenty one );
my %lookup = map { $_ => undef } @array;
for my $element ( qw( one two three ) ) {
if ( exists $lookup{ $element }) {
print "$element\n";
}
}
assuming that the number of times the element appears in @array
is not important and the contents of @array
are simple scalars.
List::Util::first
$foo = first { ($_ && $_ eq "value" } @list; # first defined value in @list
Or for hand-rolling types:
my $is_in_list = 0;
foreach my $elem (@list) {
if ($elem && $elem eq $value_to_find) {
$is_in_list = 1;
last;
}
}
if ($is_in_list) {
...
A slightly different version MIGHT be somewhat faster on very long lists:
my $is_in_list = 0;
for (my $i = 0; i < scalar(@list); ++$i) {
if ($list[i] && $list[i] eq $value_to_find) {
$is_in_list = 1;
last;
}
}
if ($is_in_list) {
...
TIMTOWTDI
sub is (&@) {
my $test = shift;
$test->() and return 1 for @_;
0
}
sub in (@) {@_}
if( is {$_ eq "a"} in qw(d c b a) ) {
print "Welcome in perl!\n";
}
List::MoreUtils
On perl >= 5.10 the smart match operator is surely the easiest way, as many others have already said.
On older versions of perl, I would instead suggest List::MoreUtils::any.
List::MoreUtils
is not a core module (some say it should be) but it's very popular and it's included in major perl distributions.
It has the following advantages:
- it returns true/false (as Python's
in
does) and not the value of the element, asList::Util::first
does (which makes it hard to test, as noted above); - unlike
grep
, it stops at the first element which passes the test (perl's smart match operator short circuits as well); - it works with any perl version (well, >= 5.00503 at least).
Here is an example which works with any searched (scalar) value, including undef
:
use List::MoreUtils qw(any);
my $value = 'test'; # or any other scalar
my @array = (1, 2, undef, 'test', 5, 6);
no warnings 'uninitialized';
if ( any { $_ eq $value } @array ) {
print "$value present\n"
}
P.S.
(In production code it's better to narrow the scope of no warnings 'uninitialized'
).
Probably Perl6::Junction
is the clearest way to do. No XS dependencies, no mess and no new perl version required.
use Perl6::Junction qw/ any /;
if (any(@grant) eq 'su') {
...
}
This blog post discusses the best answers to this question.
As a short summary, if you can install CPAN modules then the best solutions are:
if any(@ingredients) eq 'flour';
or
if @ingredients->contains('flour');
However, a more usual idiom is:
if @any { $_ eq 'flour' } @ingredients
which i find less clear.
But please don't use the first() function! It doesn't express the intent of your code at all. Don't use the "Smart match" operator: it is broken. And don't use grep() nor the solution with a hash: they iterate through the whole list. While any() will stop as soon as it finds your value.
Check out the blog post for more details.
PS: i'm answering for people who will have the same question in the future.
You can accomplish a similar enough syntax in Perl if you do some Autoload hacking.
Create a small package to handle the autoload:
package Autoloader;
use strict;
use warnings;
our $AUTOLOAD;
sub AUTOLOAD {
my $self = shift;
my ($method) = (split(/::/, $AUTOLOAD))[-1];
die "Object does not contain method '$method'" if not ref $self->{$method} eq 'CODE';
goto &{$self->{$method}};
}
1;
Then your other package or main script will contain a subroutine that returns the blessed object which gets handled by Autoload when its method attempts to be called.
sub element {
my $elem = shift;
my $sub = {
in => sub {
return if not $_[0];
# you could also implement this as any of the other suggested grep/first/any solutions already posted.
my %hash; @hash{@_} = ();
return (exists $hash{$elem}) ? 1 : ();
}
};
bless($sub, 'Autoloader');
}
This leaves you with usage looking like:
doTask if element('something')->in(@array);
If you reorganize the closure and its arguments, you can switch the syntax around the other way to make it look like this, which is a bit closer to the autobox style:
doTask if search(@array)->contains('something');
function to do that:
sub search {
my @arr = @_;
my $sub = {
contains => sub {
my $elem = shift or return;
my %hash; @hash{@arr} = ();
return (exists $hash{$elem}) ? 1 : ();
}
};
bless($sub, 'Autoloader');
}
精彩评论