in reply to finding top 10 largest files

You have file size as the key to your hash so sort just needs to be:

for my $key( sort { $b <=> $a } keys %sizehash ) { # blah }

HOWEVER -> From the practical point of view your data structure is ass about. You really want the full path as the key (unique) not the file size (not unique). For example if you have two files of 1,000,001 bytes then only the second one you find will be seen as it will overwite the filename stored with the 1,000,001 byte key.

To sort a hash numerically by value once you fix the BUG you would do:

for my $file( sort { $sizehash{$b} <=> $sizehash{$a} } keys %sizehash +) { my $size = commas($sizehash{$file}); last if $y++ > 10; print "$size: $file\n"; }

cheers

tachyon

Replies are listed 'Best First'.
Re: Re: finding top 10 largest files
by bfdi533 (Friar) on Mar 02, 2004 at 18:02 UTC

    I knew that my data structure was really mucked up but needed to get some code working. I had tried for some time to get some sort of data structure that would keep only 10 items in it but could not figure it out. So, speed of initial implementation won out.

    The comments and recommendations are well received. Thanks!

    Ed

      Amazed you replied so late! Anyway we all understand pressure....but....it is quicker to do it right the first time so you don't have to redo it later.

      Glad it helped you. Perl is a real power tool on any system. On Win32 you can for example get a file grep with a one liner like:

      perl -ne "print "$.\t$_" if m/whatever/" file.txt

      cheers

      tachyon