in reply to Find the n biggest files
How could I achieve that efficiently ? With a 2 dimensional-array ?
Yes, if you don't want to store the entire list of files for final sorting, you can incrementally update an array (or, more precisely, an AoA) that just keeps the n biggest files. That is, for every file (in your scanfile routine), you'd do
push @biggest, [$name, $size]; @biggest = sort {$a->[1] <=> $b->[1]} @biggest; shift @biggest if @biggest > $n; # remove smallest
You'll be doing quite a lot of sorting, but if the n is reasonably small, those would be small lists only, so this shouldn't be a performance problem. Also, there are ways to optimize this to avoid unnecessary sorting, but I'll leave this as an exercise...
|
|---|