【发布时间】:2013-04-25 10:18:31
【问题描述】:
我的文本文件:
Name: mak
Place: ynr
Age: 22
-------------
Name: john
Place: kkr
Age: 21
-------------
Name: mak
Place: knl
Age: 23
-------------
我正在做的是:
open(FILE, "path to file") or die "";
$check="mak";
@arr=<FILE>
for(@arr)
{
if ($_=/Name:\s(.*)/)
{
$a=$1;
if($a eq $check)
{
print "matched name"
}
#now i want if "mak" is matched then after that it should match the age and store it one variable and then compare it with other matched age with same name
}
}
我想先得到名字,如果匹配为“mak”,那么我还需要检查年龄并比较年龄。
【问题讨论】:
-
那么,到目前为止,您尝试了什么?互联网上有数百个 perl 中“匹配事物”的示例。到目前为止你的代码是什么?你到底卡在哪里了?
-
请edit您的问题添加您的代码/完善您的问题。不要不要在 cmets 中发布。
-
在你的代码中你使用 = 而不是 =~
-
你可以使用
if (/Name:\s(.*)/)匹配$_
标签: perl pattern-matching