|
|
| go ahead... be a heretic | |
| PerlMonks |
Re: help sorting hash of hashes by valueby atcroft (Abbot) |
| on Apr 08, 2022 at 01:18 UTC ( [id://11142814]=note: print w/replies, xml ) | Need Help?? |
|
If I read your code correctly, only the inner loop on $ba is sorting on values. Hash keys are not returned in a specific order, so your $foo keys may be all over the place. But your expected output really looks to be sorting on key $foo, then on $ba, so I'm not sure "sorting on values" is you are asking. If it is, I would expect the following code snippet to generate your expected output:
The output matches your expected output: $ perl 111142758a.pl foo: foo3, ba: baz, value: 0.0001939 foo: foo3, ba: bar, value: 9.966003977e-06 foo: foo2, ba: baz, value: 0 foo: foo2, ba: bar, value: -4.0183e-05 foo: foo1, ba: baz, value: -0.000398154 foo: foo1, ba: bar, value: -0.12697 If instead you are sorting on $foo decending then on $ba decending, the code would instead be:
This output also matches your expected output: $ perl 111142758b.pl foo: foo3, ba: baz, value: 0.0001939 foo: foo3, ba: bar, value: 9.966003977e-06 foo: foo2, ba: baz, value: 0 foo: foo2, ba: bar, value: -4.0183e-05 foo: foo1, ba: baz, value: -0.000398154 foo: foo1, ba: bar, value: -0.12697 If you really want your hash output sorted by values, there may be other ways but the simplest that comes to my mind is to push the order into a temporary structure you can actually sort:
This also creates your expected output: $ perl 111142758c.pl foo: foo3, ba: baz, value: 0.0001939 foo: foo3, ba: bar, value: 9.966003977e-06 foo: foo2, ba: baz, value: 0 foo: foo2, ba: bar, value: -4.0183e-05 foo: foo1, ba: baz, value: -0.000398154 foo: foo1, ba: bar, value: -0.12697 Hope that helps.
In Section
Seekers of Perl Wisdom
|
|
||||||||||||||||||||||||||||||