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?


In reply to sort file with your own logic by citi2015

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.