Iain Adams wrote:
> Hello,
> 
> I am trying to sort a hash of hashes.
> 
> my code looks like this
> 
>  foreach $cnt (sort keys %{ $relations{ $uid }{ "instances" } }){
>              print OUT "$cnt 1, ";
>  }
> 
> This prints out the correct numbers (the keys of the instances hash.
> However the sorting is a bit weird.  The keys are all integers yet
> when sorting the keys aren't put in number order. instead the first
> digit is seen as the qualifier.
> 
> Here is an example output:
> 
>  100 1, 120 1, 121 1, 122 1, 123 1, 124 1, 125 1, 126 1, 127 1, 132 1,
> 133 1, 134 1, 135 1, 136 1, 137 1, 138 1, 139 1, 221 1, 222 1, 223 1,
> 224 1, 225 1, 226 1, 227 1, 228 1, 235 1, 236 1, 237 1, 238 1, 239 1,
> 240 1, 241 1, 242 1, 323 1, 324 1, 325 1, 326 1, 327 1, 328 1, 329 1,
> 330 1, 50 1, 51 1, 52 1, 53 1, 54 1, 55 1, 56 1, 57 1, 93 1, 94 1, 95
> 1, 96 1, 97 1, 98 1, 99 1,
> 
> As you can see it is not in normal order.
> 
> Does anyone know why this is happening and how I can fix it?

I think you have had your answer: you are sorting in dictionary order instead of
numerical order. However a couple of things could have helped. First of all take
out the hash reference from your data structure before using it in later 
statements.

Iain Adams wrote:
> 
>  foreach $cnt (sort keys %{ $relations{ $uid }{ "instances" } }){
>              print OUT "$cnt 1, ";
>  }

is much more clear written as

my $instances = $relations{$uid}{instances};

foreach my $cnt (sort keys %$instances) {
  print OUT "$cnt 1, ";
}



And secondly it really shouldn't be up to the people who are trying to help you
to make up a data stucture that fits with the piece of code you've graced us
with. I did this

my %relations = (
  99 => {
    instances => {
       1 => 2,
       3 => 4,
       5 => 6,
    },
  },
);


and I'm pretty cross about having to do it.

Rob

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to