首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何对perl脚本的输出进行排序

如何对perl脚本的输出进行排序
EN

Stack Overflow用户
提问于 2013-01-28 12:48:39
回答 3查看 683关注 0票数 0

我有一个测试文件'from.xml‘,它包含以下内容:

代码语言:javascript
复制
<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>

当我在它生成的文件上运行我的脚本时,正确地生成了以下内容:

代码语言:javascript
复制
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。我需要的线路输出的时间顺序。提前谢谢。

代码语言:javascript
复制
#!/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;
EN

回答 3

Stack Overflow用户

发布于 2013-01-28 12:51:33

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

票数 1
EN

Stack Overflow用户

发布于 2013-01-28 13:01:18

存储、排序、输出。

代码语言:javascript
复制
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;

其他更改:

  • 修复了缩进问题。
  • 将其中一个行尾打印到了一个奇怪的位置,这可能是因为您之前忘记打印换行符。
  • 删除了非贪婪修饰符的使用。我讨厌它;它只会导致surprises.
  • Removed,不必要地使代码longer.
  • Removed许多无用的\.

$line的使用

也就是说,使用适当的XML解析器而不是编写自己的草率版本同样容易。

代码语言:javascript
复制
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;
票数 1
EN

Stack Overflow用户

发布于 2016-09-27 20:19:31

你可以通过一个排序例程来使用排序:

代码语言:javascript
复制
#!/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";

它打印:

代码语言:javascript
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14555605

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档