【发布时间】:2015-03-24 22:47:49
【问题描述】:
我在下面的代码中检查排除的数组中的特定变量位置。它适用于除一个以外的所有数组元素 (abc/def/libraries/linux_3.2.60-1+deb7u3.dsc) .当我提供这个元素作为我的位置时,它的打印“位置不排除”,即使它被排除在外。
我怎样才能让我的代码得到这个元素以及排除?
use strict;
use warnings;
my @excluded = (
"xyz/efg/headers/",
"abc/def/libraries/jni-mr.h",
"abc/def/libraries/linux_3.2.60-1+deb7u3.dsc",
);
my $location = "abc/def/libraries/linux_3.2.60-1+deb7u3.dsc";
my $badpath = 0;
foreach (@excluded) {
# -- Check if location is contained in excluded array
if ($location =~ /^$_/) {
$badpath = 1;
print "location is excluded : $location \n";
}
}
if (! $badpath) {
print "location is not excluded : $location \n";
}
期望的输出:
location is excluded : abc/def/libraries/linux_3.2.60-1+deb7u3.dsc
电流输出:
location is not excluded : abc/def/libraries/linux_3.2.60-1+deb7u3.dsc
【问题讨论】:
-
如果您不打算应用任何正则表达式元字符,您最好使用
index函数。