|
|
Re: sort method on hashes: msg#00113
|
Subject: |
Re: sort method on hashes |
I wrote:
which doesn't seem to be sorted in order of the hash key $sccs (i.e.
the order is d.2.1.3 - b.1.1.1 - a.45.1.1 - a.1.1.2 - c.37.1.8 -
b.74.1.1 whereas I would have expected a.1.1.2 - a.45.1.1 - b.1.1.1 -
b.74.1.1 - c.37.1.8 - d.2.1.3 Can anybody see what I'm doing wrong?
Tom Insam replied:
The docs state that sort returns the keys sorted in the order of the
hash _values_, which in this case will be... uh... well, not very
sensible, if I read your data structure correctly. Try [% FOREACH code =
s_to_c.$sccs.keys.sort %].
Thanks, Tom! I've read the doc several times and somehow never noticed
that it can even be read in the way you say, but you're right, that
fixes it. (well [% FOREACH sccs = s_to_c.keys.sort %] does :)
I'm curious why it behaves that way instead of the always valid and (to
me anyway) much more obvious semantics of key order? Especially given
that you can always do hash.values.sort if the values are sensible to
sort on, unlike the hash-ref addresses - I guess - in my case.
Thanks again, Dave
|
| |