I have an array containing some PCRE matching patterns (like "prefix_.*") and I'm comparing a string with all the patterns of the array. Currently, I'm working with this code:
我有一个包含一些PCRE匹配模式的数组(比如“prefix _。*”),我正在将一个字符串与数组的所有模式进行比较。目前,我正在使用此代码:
foreach (@matchingPatterns) {
if ("$string" =~ "$_") {
[do something]
}
}
This code is working well but I'm sure that there is a prettier way to do that in Perl (without any loop?).
这段代码运行良好,但我确信在Perl中有一个更漂亮的方法(没有任何循环?)。
Am I wrong? ;)
我错了吗? ;)
1 个解决方案
#1
1
There's not much scope for improvement here, but I'd be more liable to write something like one of the following:
这里没有太大的改进余地,但我更倾向于写下类似下列内容之一:
for (@matchingPatterns) {
next if $string !~ /$_/;
# do something
}
or
要么
for (grep { $string =~ /$_/ } @matchingPatterns) {
# do something
}
...both of which at least save you a few lines of code.
...这两者至少可以节省几行代码。
#1
1
There's not much scope for improvement here, but I'd be more liable to write something like one of the following:
这里没有太大的改进余地,但我更倾向于写下类似下列内容之一:
for (@matchingPatterns) {
next if $string !~ /$_/;
# do something
}
or
要么
for (grep { $string =~ /$_/ } @matchingPatterns) {
# do something
}
...both of which at least save you a few lines of code.
...这两者至少可以节省几行代码。