Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re^6: setting PERL_PERTURB_KEYS & PERL_HASH_SEED in a perl file

by haukex (Archbishop)
on Jul 18, 2016 at 13:21 UTC ( [id://1167966]=note: print w/replies, xml ) Need Help??


in reply to Re^5: setting PERL_PERTURB_KEYS & PERL_HASH_SEED in a perl file
in thread setting PERL_PERTURB_KEYS & PERL_HASH_SEED in a perl file

Hi gravid,

Yep, there's a better solution than messing with Perl's internals :-)

my %hash = ( a=>0, b=>1, c=>0, d=>3 ); foreach my $k ( sort { $hash{$a} <=> $hash{$b} or $a cmp $b } keys %ha +sh ) { print "$k = $hash{$k}\n"; }

Always outputs:

a = 0 c = 0 b = 1 d = 3

Background: If the comparison of the values (<=>) shows they are equal it returns zero, so then the second part of the or expression is evaluated, comparing the keys.

Update: See the multi-field sort in How do I sort an array by (anything)? That FAQ answer also references this article: Far More Than Everything You've Ever Wanted to Know About Sorting

Hope this helps,
-- Hauke D

Replies are listed 'Best First'.
Re^7: setting PERL_PERTURB_KEYS & PERL_HASH_SEED in a perl file
by gravid (Novice) on Jul 18, 2016 at 13:31 UTC

    Thx Hauke!

    This is the solution.

    Works for me great :)

    Thx

    Guy

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1167966]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2024-04-25 23:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found