citi2015 has asked for the wisdom of the Perl Monks concerning the following question:
I want to sort the output from du -sh, from the big size to small size, here is an example
33G /data/A 37M /data/B 44G /data/C 46G /data/D 68G /data/E 114G /data/F 148G /data/G 169M /data/H
her is my test code
my $file_dist="../data/disk_usage.dat.new.bkp"; ##open the usage file open(my $fh ,$file_dist) or die "can not open file"; my @arr = (); my @sorted = (); while(my $line=<$fh>){ chomp $line; push(@arr, $line); } @sorted = sort sort_arr @arr; print Dumper \@sorted; sub sort_arr { my ($size_a, $type_a); if($a =~ /(\d*?\.*\d*)([M|G|K])/){ $size_a = $1; $type_a = $2; } my ($type_b, $size_b); if($b =~ /(\d*?\.*\d*)([M|G|K])/){ $size_b = $1; $type_b = $2; } my $a1 = return_base($type_a) * $size_a; my $b1 = return_base($type_b) * $size_b; $b1 <=> $a1 ; } sub return_base{ my $type = shift; return 1024 if ($type eq "M"); return 1024*1024 if($type eq "G"); return 1 if($type eq "K"); } sub trim{ my $s=shift; $s=~s/\s+$//; $s=~s/^\s+//; return $s; }
My question is why in sub sort_arr, I don't need to use defination, like my $a, my $b?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: sort file with your own logic
by Discipulus (Canon) on Mar 27, 2015 at 08:46 UTC | |
|
Re: sort file with your own logic
by hdb (Monsignor) on Mar 27, 2015 at 09:11 UTC | |
|
Re: sort file with your own logic
by johngg (Canon) on Mar 27, 2015 at 10:26 UTC | |
by Anonymous Monk on Mar 27, 2015 at 19:57 UTC | |
|
Re: sort file with your own logic
by Laurent_R (Canon) on Mar 27, 2015 at 23:16 UTC |