Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
是否有一个“perl”;不在「;操作人员_Perl - Fatal编程技术网

是否有一个“perl”;不在「;操作人员

是否有一个“perl”;不在「;操作人员,perl,Perl,假设我有一个数字数组,并且我想确保所有这些都属于其中一个集合(x,y,z),我当前正在检查以下值是否为0: scalar ( grep { $_ ne x && $_ ne y && $_ ne z } @arr ) 我只是想知道,如果我们在perl中也有类似于sql的“IN”和“not IN”操作符,是否会更容易 scalar ( grep { $_ NOT IN (x,y,z) } @arr ) 还是已经有了 谢谢, 三位一体 解决这个问题的典型方法是使用

假设我有一个数字数组,并且我想确保所有这些都属于其中一个集合(x,y,z),我当前正在检查以下值是否为0:

scalar ( grep { $_ ne x && $_ ne y && $_ ne z } @arr )
我只是想知道,如果我们在perl中也有类似于sql的“IN”和“not IN”操作符,是否会更容易

scalar ( grep { $_ NOT IN (x,y,z) } @arr )
还是已经有了

谢谢, 三位一体


解决这个问题的典型方法是使用哈希:

my %set = map {$_ => 1} qw( x y z ); # add x, y and z to the hash as keys
                                     # each with a value of 1

my @not_in_set = grep {not $set{$_}} @arr;
在这里,库或对于测试列表成员身份非常有用,因为您不关心值本身,只关心是否存在。它们比grep更有效,因为一旦找到匹配项,它们就停止在列表中循环,这确实可以加快长列表的速度。此外,这些模块是用C/XS编写的,比任何纯perl实现都要快

use List::MoreUtils 'any';

my @list = qw(foo bar baz);

my $exists = any { $_ eq 'foo' } @list;
print 'foo ', ($exists ? 'is' : 'is not'), " a member of the list\n";

$exists = any { $_ eq 'blah' } @list;
print 'blah ', ($exists ? 'is' : 'is not'), " a member of the list\n";

(如果您仅限于使用core Perl附带的模块,那么您可以先使用List::Util中的
——它是Perl在5.7.3中首次附带的。)

如果您使用的是Perl 5.10或更高版本(或者愿意使用Perl 5.18及更高版本中的实验性功能),智能匹配操作符将完全满足您的要求

# if Perl 5.18 or higher; otherwise not needed
no warnings 'experimental::smartmatch';

my @filter = qw(X Y Z);
my $not_in_filter = scalar grep { ! ($_ ~~ @filter) } @array;
如果过滤器和/或@array很大,则可能会比较慢,因为它是O(N^2)。在这种情况下,您仍然可以使用智能匹配,只需更改过滤器:

my %filter = map { $_ => 1 } qw(X Y Z);
my $not_in_filter = scalar grep { ! ($_ ~~ %filter) } @array;
有关更多信息,请参见《perldoc perlsyn》中的

此外,如果需要支持5.10到5.18之间的Perl版本,请考虑使用CPAN模块<代码>实验< /代码>。如果找到需要它的Perl版本,它会进行版本检查并包括“无警告”

use experimental 'smartmatch';

请参阅:

如果阵列中的不同内容少于几百万,也可以使用 使用散列设置差异的教科书方法:

my %seen;
@seen{ @arr } = (); # Create a key for every distinct value in @arr
delete @seen{ qw(x y z) }; # And remove the ones for x, y, and z

if (keys %seen) {
    # There was something in @arr that's not x, y, or z
} else {
    # There wasn't
}
~~在数组中

my $target = 'bar';
my @look_in = ('foo','baz','bar','etc');
if( not $target ~~ @look_in) {
    print "target is not in array";
}

这被称为smartmatch,有些人建议不要使用它们,但它们可以很好地处理字符串列表

谢谢@Robert,~~对我来说是新鲜事@trinity,不幸的是,smartmatch系列功能在perl 5.18.0中被标记为实验性的,因此您可能不应该开始使用它
my $target = 'bar';
my @look_in = ('foo','baz','bar','etc');
if( $target ~~ @look_in) {
    print "target is in array ";
}
my $target = 'bar';
my @look_in = ('foo','baz','bar','etc');
if( not $target ~~ @look_in) {
    print "target is not in array";
}