如何从perl脚本中排序输出

时间:2013-01-28 04:48:39

标签: perl

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

正如您在上面所看到的,这就是我的目标,但在现实世界的应用程序中,时间是乱序的。有没有办法对这个输出进行排序?我已经搜索过,并且没有任何明确的结论。我创建了这个,我是编程和esp Perl的菜鸟。我需要以chron顺序输出的行。提前谢谢。

#!/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;

3 个答案:

答案 0 :(得分:1)

而不是打印,将其存储在数组和sort it first中。

答案 1 :(得分: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解析器而不是编写自己的slapdash版本同样容易。

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;

答案 2 :(得分:0)

您可以使用排序例程进行排序:

<!DOCTYPE html>
<html>

  <head>
    <script data-require="react@*" data-semver="0.13.3" src="http://cdnjs.cloudflare.com/ajax/libs/react/0.13.3/react.js"></script>
    <script data-require="react-jsx@*" data-semver="0.13.1" src="http://cdnjs.cloudflare.com/ajax/libs/react/0.13.1/JSXTransformer.js"></script>

    <link rel="stylesheet" href="style.css" />
    <script type="text/jsx">

      var App = React.createClass({
          getInitialState: function () {
              return {value: 'Hello!'};
          },

          changeTo: function (e) {
              this.setState({value: e.target.value});
          },

          render: function () {
              return (
                  <div>{this.state.value}
                      <input type="text" defaultValue={this.state.value || ''} 
                         value={this.state.value} onBlur={(e) => this.changeTo(e)} />
                  </div>
              );
          }
      });

      React.render(
          <App />,
          document.getElementById('app')
      );
    </script>
  </head>

  <body>
    <div id="app"></div>
  </body>

</html>

打印:

#!/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";