0

我有一个测试文件“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;
4

3 回答 3

1

与其打印,不如将其存储在一个数组中并首先对其进行排序

于 2013-01-28T04:51:32.643 回答
1

存储、排序、输出。

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;
于 2013-01-28T05:01:18.180 回答
0

您可以将 sort 与排序例程一起使用:

#!/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 second
于 2016-09-27T12:19:31.217 回答