我有一个测试文件“from.xml”,其中包含以下内容:
<target>
<promptlist>
<prompt address="EXA112" time="00:11:20.00">This is what I want to see first.second</prompt>
<prompt address="EXA222" time="00:22:20.00">This is what I want to see second</prompt>
</promptlist>
</target>
<target>
<promptlist>
<prompt address="EXA444" time="00:44:40.00">This is what I want to see fourth</prompt>
<prompt address="EXA333" time="00:33:30.00">This is what I want to see third</prompt>
<prompt address="EXA555" time="00:55:50.00">This is what I want to see fifth</prompt>
<prompt address="EXA111" time="00:11:10.00">This is what I want to see first</prompt>
<prompt address="EXA666" time="00:66:60.00">This is what I want to see sixth</prompt>
</promptlist>
</target>
当我在文件上运行我的脚本时,它会正确生成以下内容:
00:11:20.00 EXA112 This is what I want to see first.second
00:22:20.00 EXA222 This is what I want to see second
00:44:40.00 EXA444 This is what I want to see fourth
00:33:30.00 EXA333 This is what I want to see third
00:55:50.00 EXA555 This is what I want to see fifth
00:11:10.00 EXA111 This is what I want to see first
00:66:60.00 EXA666 This is what I want to see sixth
正如你在上面看到的,这就是我的目标,但在现实世界的应用程序中,时间是乱序的。有没有办法对这个输出进行排序?我已经搜索过,但没有什么清楚的。我已经创建了这个,我是编程和尤其是 Perl 的菜鸟。我需要按时间顺序输出的行。提前致谢。
#!/usr/bin/perl
use strict;
use warnings;
use diagnostics -verbose;
my $filename = $ARGV [0];
open( my $fh, '<', $filename ) or die "Can't open $filename: $!";
while ( my $line = <$fh> ) {
if ( $line =~ /\<prompt / ) {
if ( $line =~ /time=\"(.+?)\"/ ) {
print"\n $1 ";
if ( $line =~ /address=\"(.+?)\"/ ) {
print"$1 ";
if ( $line =~ /\>(.+?)\</ ) {
print"$1\n\n ";
}
}
}
}
}
close $fh;