我是Perl的新手,我在编写一个能成功解析结构化文本文件的Perl脚本时遇到了极大的困难。
基本上该文件包含与以下内容类似的文本:
//test/some_text/value1
... #001 some non important text
... #023 some non important text
//test/some_text/value2
... #1123 some non important text
//test/some_text/value3
... #001 some non important text
... #023 some non important text
... #022 some non important text
... #566 some non important text
//test/some_text/value4
... #42 some non important text
不幸的是我无法预测带有点数的线条我将会以//开头的每一行开头,我希望得到这样的输出:
//test/some_text/value1 - 001
//test/some_text/value1 - 023
//test/some_text/value2 - 1123
//test/some_text/value3 - 001
//test/some_text/value3 - 023
//test/some_text/value3 - 022
//test/some_text/value3 - 566
//test/some_text/value4 - 42
我正在逐行阅读文本文件,但我觉得这不是最佳解决方案。我非常感谢你提示并帮助解决问题。
答案 0 :(得分:3)
逐行是可以的。您只需要存储前缀行。
use warnings;
use strict;
my $prefix;
while (<DATA>) {
if (m{^//}) {
chomp;
$prefix = $_;
}
else {
my ($num) = /#(\d+)/;
print "$prefix - $num\n";
}
}
__DATA__
//test/some_text/value1
... #001 some non important text
... #023 some non important text
//test/some_text/value2
... #1123 some non important text
//test/some_text/value3
... #001 some non important text
... #023 some non important text
... #022 some non important text
... #566 some non important text
//test/some_text/value4
... #42 some non important text