in reply to Re^2: How can I sort this data on the fourth field?
in thread How can I sort this data on the fourth field?

You need to build an array (with push @data, $line;) inside your while loop, between that next and the closing brace.

The sort snippet I posted should be placed after the closing brace of the while.

Something like:

my @data; while (my $line = <$fh>) { chomp($line); last if ($line =~ /TOTAL COMMAND SUMMARY/); next if (($line =~ /^[a-zA-Z]/) or ($line =~ /^\s*$/) or ($line =~ / +^\s/)); push @data, $line; } my @sorted = map { $_->[0] } sort { $a->[1] <=> $b->[1] } map [ $_, (split)[3] ], @data;

Going on the data you posted, wouldn't that next be better written as:

next unless $line =~ /^\d/;

Replies are listed 'Best First'.
Re^4: How can I sort this data on the fourth field?
by country1 (Acolyte) on Aug 16, 2007 at 14:23 UTC


    I am executing the following code per your recommendations
    and am getting no print output.

    #!/usr/bin/perl use strict; use warnings; ########################################################## # # Parse PS Accounting Data # ########################################################## my $uid; my $login; my $cpupr; my %someData; my $value; my $key; for my $file ("may_07_xsd00544.dat") { open (my $fh,"<",$file) or die "Can't open file $file: $!"; my @data; while (my $line = <$fh>) { chomp($line); chomp ( my @data = <$fh> ); last if ($line =~ /TOTAL COMMAND SUMMARY/); next unless $line =~ /^\d/; push @data, $line; } my @sorted = map { $_->[0] } sort { $b->[1] <=> $a->[1] } map [ $_, (split)[3] ], @data; print "$_\n" for @sorted; close $fh or die "Can't close result file: $!"; }


    I modified the sort code to do a descending numeric sort on the 4th column.

      You've put an extra line in. Remove the chomp ( my @data = <$fh> ); line