同意楼上的。 不过你的代码在我这里第二次打印 $1时,会直接报一个错误:
Use of uninitialized value $1 in print at reg.pl line 21. 难道不同版本的perl对于此种情况的处理不一样? 2009/7/22 Lee Duhem <[email protected]> > 2009/7/22 ken <[email protected]>: > > > > #!/usr/bin/perl > > use strict; > > use warnings; > > > > my $txt1="slot 0/3/0/0/0"; > > my $txt2="slot 0/4/0/0/0"; > > > > my @addrs; > > foreach($txt1,$txt2) > > { > > my @tmp=split ' '; > > push @addrs,$tmp[1]; > > } > > > > my $txt3="0/3/0/0/0/0 CLAIMED"; > > > > foreach my $addr (@addrs) > > { > > $txt3 =~ /$addr\/0\s+(.*)/; > > print $1,"\n"; > > } > > > > > > 上面这个程序在txt3中匹配0/3/0/0/0和0/4/0/0/0。 > > 程序输出两次CLAIMED,可是第二次明明就不匹配啊,为什么呢 > > 正因为第二次匹配失败,所以 $1 的值根本就没有变化。你应该写成 > > if ($txt3 =~ /$addr\0\s+(.*)/) { > print $1, "\n"; > } > > 只有在匹配成功后才打印。 > > lee > > > > -- 姜源 Yuan Jiang http://blog.vetcafe.net http://twitter.com/sleetdrop --~--~---------~--~----~------------~-------~--~----~ 您收到此信息是由于您订阅了 Google 论坛“PerlChina Mongers 讨论组”论坛。 要在此论坛发帖,请发电子邮件到 [email protected] 要退订此论坛,请发邮件至 [email protected] 更多选项,请通过 http://groups.google.com/group/perlchina?hl=zh-CN 访问该论坛 -~----------~----~----~----~------~----~------~--~---
