in reply to to sort hash wrt values corresponding to keys
It sounds like you want a hash of lists. Check out perldsc. When you write your sort function you can tell it to use an element of the child structure.
I hope I'm not doing your homework for you, but because I'm bored, beneath the spoiler is an attempt at the solution to the problem as I understand it. HTH.
my %hash = ( arr1 => [1, 2, 3, 'a'], arr2 => [2, 3, 4, 'x'], arr3 => [3, 4, 5, 'e'], arr4 => [4, 5, 6, 'b'] ); my @sorted_hash_keys = sort { $hash{$a}->[3] cmp $hash{$b}->[3] } keys %hash; foreach my $key (@sorted_hash_keys) { print "\$hash{$key} = [@{$hash{$key}}]\n"; }
Untested, but should output:
$hash{arr1} = [1 2 3 a] $hash{arr4} = [4 5 6 b] $hash{arr3} = [3 4 5 e] $hash{arr2} = [2 3 4 x]
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: to sort hash wrt values corresponding to keys
by ramprasad27 (Sexton) on Nov 02, 2011 at 06:28 UTC | |
by ansh batra (Friar) on Nov 02, 2011 at 06:36 UTC | |
|
Re^2: to sort hash wrt values corresponding to keys
by ansh batra (Friar) on Nov 02, 2011 at 06:17 UTC | |
by GrandFather (Saint) on Nov 02, 2011 at 06:27 UTC | |
by ansh batra (Friar) on Nov 02, 2011 at 06:32 UTC |