我有一个测试文件'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;发布于 2013-01-28 12:51:33
将其存储在数组和sort it first中,而不是打印。
发布于 2013-01-28 13:01:18
存储、排序、输出。
my @data;
while ( <$fh> ) {
if ( /<prompt / ) {
if ( /time="([^"]+)"/ ) {
my $time = $1;
if ( /address="([^"]+)"/ ) {
$addr = $1;
if ( />([^<]+)</ ) {
push @data, "$time $addr $1\n\n\n";
}
}
}
}
}
print for sort @data;其他更改:
\.的$line的使用
也就是说,使用适当的XML解析器而不是编写自己的草率版本同样容易。
use XML::LibXML qw( );
my $parser = XML::LibXML->new();
my $doc = $parser->parse_file($ARGV[0]);
my $root = $doc->documentElement();
my @data;
for my $prompt_node ($root->findnodes('/target/promptlist/prompt')) {
my $time = $prompt_node->getAttribute('time');
my $addr = $prompt_node->getAttribute('address');
my $prompt = $prompt_node->textContent();
push @data, "$time $addr $prompt\n\n\n";
}
print for sort @data;发布于 2016-09-27 20:19:31
你可以通过一个排序例程来使用排序:
#!/usr/bin/perl
use strict;
use warnings;
print "Before sort:\n";
my @s = ("00:22:20.00 EXA222 This is what I want to see second", "00:11:20.00 EXA112 This is what I want to see first.second");
print "$s[0]\n";
print "$s[1]\n";
@s = sort {substr($a, 0, 11) cmp substr($b, 0, 11)}(@s);
print "After sort:\n";
print "$s[0]\n";
print "$s[1]\n";它打印:
Before sort:
00:22:20.00 EXA222 This is what I want to see second
00:11:20.00 EXA112 This is what I want to see first.second
After sort:
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 secondhttps://stackoverflow.com/questions/14555605
复制相似问题