in reply to sorting arrays in respect to a different hash's values
Maybe this little example helps.
#!/usr/bin/perl -l use strict; use warnings; my %hash = ( foo => 1, whatever => { rank => 11 }, you => { rank => 2 }, want => { rank => 7 }, ); my @arr = grep { $_ ne 'foo' } keys %hash; my @sorted = sort { $hash{$a}->{rank} <=> $hash{$b}->{rank} } @arr; print "@arr"; print "@sorted";
Update:
modified test data in code
If your hash values are plain strings, than you just need to do something like:
my @sorted = sort { $hash{$a} cmp $hash{$b} } @arr;
See sort for more information about sort and the ways, how you can influence the sort.
Also see Schwartzian Transform
Update2: replaced <=> with cmp in second example; Thanks to AnomalousMonk and roboticus for pointing me on that.
|
---|
In Section
Seekers of Perl Wisdom