in reply to Re: Hash sorting
in thread Hash sorting

Very good. But how about
my @data; foreach my $section (keys %hash) { foreach my $item (keys %{$hash{$section}}) { push @data, printf "%5d %7s %7s\n", $hash{$section}{$item}, $secti +on, $item; } } print sort @data;

jdporter
The 6th Rule of Perl Club is -- There is no Rule #6.

Replies are listed 'Best First'.
Re: Re: Re: Hash sorting
by tilly (Archbishop) on May 12, 2003 at 19:56 UTC
    Make the printf a sprintf and it works.

    But the point of my post was to demonstrate how building up the array of structures allows you to always find your way through the logic. Which is why I didn't use any tricks to find my way through the logic, didn't nest maps, etc.

Re: Re: Re: Hash sorting
by Util (Priest) on May 13, 2003 at 05:09 UTC
    I often use this technique in small data-munging scripts. It is a fast variant of the Guttman Rosler Transform, and shares these limitations with GRT:
    • You must know the maximum size of each data element. For example, if the count is more than 5 digits, then the keys will not align, and the sort will be wrong.
    • You cannot use it to mix ascending and descending sorts. For example, tilly sorted the count descending, but section and item ascending. GRT can't do that.

      Bah, those are easy to deal with in a GRT.

      For the first, instead of using sprintf "%05d", $int, you can use pack "N", $int (for any positive integers that fit in a long) or even sprintf "%02d-%d", length(0+$int), $int (for positive integers of 99 digits or fewer!).

      For the latter, ~ is very handy.

                      - tye