user2016901
user2016901

Reputation: 11

how to sort output from perl script

I have a test file 'from.xml' which contains the following:

<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>

When i run my script on the file it produces, correctly, the following:

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

As you can see above, this is what I was aiming for, but as in the real world application, the times are out of order. Is there some way to sort this output? I have searched and can come up with nothing clear. I have created this, I am a noob to programming and esp Perl. I need the lines to output in chron order. Thanks in advance.

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

Upvotes: 0

Views: 767

Answers (3)

Jjj
Jjj

Reputation: 11

You could use sort with a sort routine:

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

It prints:

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

Upvotes: 0

ikegami
ikegami

Reputation: 385655

Store, sort, output.

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;

Other changes:

  • Fixed indenting.
  • One of the line endings was being printed in a weird spot, probably because you forgot to print a newline earlier on.
  • Removed the use of the non-greedy modifier. I hate it; it just leads to surprises.
  • Removed the use of $line that needlessly made the code longer.
  • Removed lots of useless \.

That said, it's just as easy using a proper XML parser instead of writing your own slapdash version.

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;

Upvotes: 1

Karthik T
Karthik T

Reputation: 31952

Instead of printing, store it in an array and sort it first.

Upvotes: 1

Related Questions