DanEichenwald has asked for the wisdom of the Perl Monks concerning the following question:

Monks; I'm trying to sort my compilers map file by memory type (region) and then by module size. To read everything in, I do this:
while (<>) { ... @fields = split ; #field[0] is the module. $entries{$region}{$fields[0]}{entry} = $fields[0] ; $entries{$region}{$fields[0]}{addr} = $fields[1] ; $entries{$region}{$fields[0]}{size} = $fields[2] ; $entries{$region}{$fields[0]}{type} = $fields[3] ; $entries{$region}{$fields[0]}{object} = $fields[5] ; ... }
This works fine and I've verified I have all of my data read in correctly. Now to sort. This will correctly sort by region:
for my $region (keys %entries) { ... }
This will then correctly sort by module:
for my $region (keys %entries) { for my $module (keys %{$entries{$region}}) { ... } }
But then I can't sort by size. I've tried the following:
for my $size (sort {%{$entries{$region}{$a}{size}} <=> %{$entries{$reg +ion}{$b}{size}}} keys %{$entries{$region}}) { ... }
I've tried a bunch of other syntaxes as well. And I always get "Can't use string ("0x...") as a HASH ref while "strict refs" in use at line ... (the last 'for' line). Any ideas what I'm doing wrong? Thanks in advance. Dan

Replies are listed 'Best First'.
Re: How do I sort hash of hashes of hashes
by moritz (Cardinal) on Dec 21, 2010 at 21:24 UTC
    Based on your code I'd think that $entries{$region}{$a}{size} is not a hash ref, so may you just leave out the %{...} hash dereferencing around it?
      Thanks so much! So obvious now!