3

私のテキストファイル:

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」と一致した場合は、年齢を確認して年齢も比較する必要があります。

4

3 に答える 3

2

これにアプローチする方法はたくさんありますが、簡潔さの点で私が気に入っている方法を次に示します。

use File::Slurp;
@records=split('---',read_file('file.txt'))

$records[0]たとえば、次のものが含まれます。

Name: mak
Place: ynr
Age: 22

分割パターンが完全ではないため、 「ジャンク」エントリが含まれます@recordsが、それは問題ではありません。これで、反復し@recordsて必要なものを見つけることができます。

于 2013-05-01T12:33:50.930 に答える