提前祝大家新年好。如何從Perl中的文件中讀取自定義的模式?
我有一個模式參數,結果和stderr(stderr可以是多行)內容的錯誤日誌文件。
$cat error_log
<parameter>:test_tot_count
<result>:1
<stderr>:Expected "test_tot_count=2" and the actual value is 3
test_tot_count = 3
<parameter>:test_one_count
<result>:0
<stderr>:Expected "test_one_count=2" and the actual value is 0
test_one_count = 0
<parameter>:test_two_count
<result>:4
<stderr>:Expected "test_two_count=2" and the actual value is 4
test_two_count = 4
...
我需要在Perl中編寫一個函數來將每個參數,結果和stderr存儲在數組或散列表中。
這是我們自己內部定義的結構。我這樣寫了Perl函數。使用正則表達式本身是否有更好的方法?
my $err_msg = "";
while (<ERR_LOG>)
{
if (/<parameter>:/)
{
s/<parameter>://;
push @parameter, $_;
}
elsif (/<result>:/)
{
s/<result>://;
push @result, $_;
}
elsif (/<stderr>:/)
{
if (length($err_msg) > 0)
{
push @stderr, $err_msg;
}
s/<stderr>://;
$err_msg = $_;
}
else
{
$err_msg .= $_;
}
}
if (length($err_msg) > 0)
{
push @stderr, $err_msg;
}
我喜歡的第一個。這是一個很好很緊的重構。 +1 – Axeman 2008-12-31 18:33:40