roadtest has asked for the wisdom of the Perl Monks concerning the following question:

Hello gurus,

I wrote following code generating report as below.

Here is code:

use warnings; use strict; use Time::Local; my ($ReadOp, $WriteOp); while (<DATA>) { next if ($. == 1); chomp; my @output = split/,\s?/; if ( $output[2] eq "W" ) { $WriteOp -> {$output[1]}{$output[4]} += $output[3]; } elsif ( $output[2] eq "R") { $ReadOp -> {$output[1]}{$output[4]} += $output[3]; } } print "=======\n"; print "WriteOperation\n"; print "=======\n"; for my $ip(keys %{$WriteOp}) { my $files= ${$WriteOp}{$ip}; for my $file(keys %{$files}) { print "$ip - $file - ${$WriteOp}{$ip}{$file}\n"; } } print "\n\n\n"; print "=======\n"; print "ReadOperation \n"; print "=======\n"; for my $ip(keys %{$ReadOp}) { my $files= ${$ReadOp}{$ip}; for my $file(keys %{$files}) { print "$ip - $file - ${$ReadOp}{$ip}{$file}\n"; } } __DATA__ Sun Aug 21 12:08:21 2011,172.22.32.28, W, 4964,/export/file1.log Sun Aug 21 12:08:21 2011,172.22.32.28, W, 4964,/export/file2.log Sun Aug 21 12:08:21 2011,172.22.32.28, W, 4963,/export/file2.log Sun Aug 21 12:08:21 2011,172.22.112.141, W, 9292,/export/home/another. +file Sun Aug 21 12:08:21 2011,172.22.32.28, W, 4964,/export/file2.log Sun Aug 21 12:08:21 2011,172.22.32.28, W, 2493,/export/file1.log Sun Aug 21 12:08:21 2011,172.22.32.28, W, 2355,/export/another.log Sun Aug 21 12:08:21 2011,172.22.32.28, R, 25,/export/another.log Sun Aug 21 12:08:21 2011,172.20.220.38, W, 3699,/export/file2.log Sun Aug 21 12:08:21 2011,172.20.220.146, W, 1996,<?> Sun Aug 21 12:08:21 2011,172.22.32.28, W, 2776,/export/file1.log Sun Aug 21 12:08:21 2011,172.22.32.28, R, 26,/export/file1.log

Here is output $./1.pl

======= WriteOperation ======= 172.20.220.38 - /export/file2.log - 3699 172.22.112.141 - /export/home/another.file - 9292 172.20.220.146 - <?> - 1996 172.22.32.28 - /export/file2.log - 14891 172.22.32.28 - /export/another.log - 2355 172.22.32.28 - /export/file1.log - 5269 ======= ReadOperation ======= 172.22.32.28 - /export/another.log - 25 172.22.32.28 - /export/file1.log - 26

My question is how I can sort the output according to the last column. I am doing following. But I am sure there is better way in perl to achieve it.

$./1.pl | sort -n -t"-" -k3

Thanks,

Replies are listed 'Best First'.
Re: sort question
by GrandFather (Saint) on Aug 28, 2011 at 00:59 UTC

    Something like this maybe:

    use warnings; use strict; my %ops; my %opType = (W => 'Write', R => 'Read'); while (<DATA>) { next if ($. == 1); chomp; my ($date, $ip, $op, $hits, $file) = split /,\s*/; next if !exists $opType{$op}; $ops{$opType{$op}}{$ip}{$file} += $hits; } report ($_, $ops{$_}) for 'Write', 'Read'; sub report { my ($type, $op) = @_; my @lines; print "\n\n\n"; print "=======\n"; print "${type}Operation \n"; print "=======\n"; for my $ip (keys %$op) { my $files = $op->{$ip}; for my $file (keys %{$files}) { my $hits = $op->{$ip}{$file}; push @lines, ["$ip - $file - $hits\n", $hits]; } } print $_->[0] for sort {$a->[1] <=> $b->[1]} @lines; } __DATA__ Sun Aug 21 12:08:21 2011,172.22.32.28, W, 4964,/export/file1.log Sun Aug 21 12:08:21 2011,172.22.32.28, W, 4964,/export/file2.log Sun Aug 21 12:08:21 2011,172.22.32.28, W, 4963,/export/file2.log Sun Aug 21 12:08:21 2011,172.22.112.141, W, 9292,/export/home/another. +file Sun Aug 21 12:08:21 2011,172.22.32.28, W, 4964,/export/file2.log Sun Aug 21 12:08:21 2011,172.22.32.28, W, 2493,/export/file1.log Sun Aug 21 12:08:21 2011,172.22.32.28, W, 2355,/export/another.log Sun Aug 21 12:08:21 2011,172.22.32.28, R, 25,/export/another.log Sun Aug 21 12:08:21 2011,172.20.220.38, W, 3699,/export/file2.log Sun Aug 21 12:08:21 2011,172.20.220.146, W, 1996,<?> Sun Aug 21 12:08:21 2011,172.22.32.28, W, 2776,/export/file1.log Sun Aug 21 12:08:21 2011,172.22.32.28, R, 26,/export/file1.log

    Prints:

    ======= WriteOperation ======= 172.20.220.146 - <?> - 1996 172.22.32.28 - /export/another.log - 2355 172.20.220.38 - /export/file2.log - 3699 172.22.32.28 - /export/file1.log - 5269 172.22.112.141 - /export/home/another.file - 9292 172.22.32.28 - /export/file2.log - 14891 ======= ReadOperation ======= 172.22.32.28 - /export/another.log - 25 172.22.32.28 - /export/file1.log - 26

    I've "cleaned up" the rest of your code too to remove code duplication and make the intent clearer (although I may have misunderstood some of the fields which actually makes the intent less clear!).

    True laziness is hard work
      Thanks for rewrite the code. This is a good sample code. Appreciate! Have a good weekend!

      cheers,