我得到了一些行为,我不理解的Perl:
>>> my @words = ('hello', 'there');
>>> $words[0] =~ /(el)/; print $1;
el
>>> $words[1] =~ /(el)/; print $1;
undef
,但在一个循环:
>>> my @words = ('hello', 'there');
>>> foreach my $word (@words){
>>> $word =~ /(el)/;
>>> print "$1\n";
>>> }
el
el
这是怎么回事?以及如何我,在循环中,得到$ 1是不确定的,当它没有在最近的正则表达式匹配,所以这样的事情会工作:
foreach my $word (@words) {
$word =~ /(el)/;
if ($1) {
print "$word matched\n";
} else {
print "$word did not match\n";
}
}
我使用的逻辑是有点不仅仅是印刷更复杂比赛。这是我用来试图了解发生了什么的简化情况。有什么办法可以做我想做的事吗?即每当我得到一个匹配时设置'$ 1','$ 2'(或别的东西),如果我没有得到匹配,每次迭代都将它们设置为'undef'? – ewok
'my($ capture)= $ word =〜/(el)/'和'my $ capture = $ word =〜/(el)/? $ 1:undef;'设置'$ capture'以你所描述的想要设置'$ 1'的方式。 – ikegami
完美。这是否意味着我可以使用'my @matches = $ word =〜/(list)(of)(match)(patterns)/;'来获取匹配数组? – ewok