John W. Krahn wrote:
Hardly Armchair wrote:
Hello List,
Hello,
I have a data structure like so:
%p_mod = {
^
You are using the wrong punctuation. That would produce a warning if you had
warnings enabled.
Sorry. I'm actually generating this data structure dynamically and just
copied the output of Data::Dumper, which treats it as a reference
instead of a hash. So, no warnings, but thanks for spotting that.
'A' => {
'fingers' => {
'4' => 'ABSFMQS',
'5' => 'SMTFQNL',
},
'name' => '8-H34'
},
'C' => {
'fingers' => {
'1' => 'ALEJIEK',
'2' => 'BESLERJ',
},
'name' => '9-J09'
},
'B' => {
'fingers' => {
'3' => 'OLPWJEK',
},
'name' => '6-G79'
}
}
To access the keys to the hash reference under 'fingers' ordered by the
interior number (1 through 5) I have made this construct:
foreach my $mods (reverse sort keys %p_mod) { #first sort to go C,B,A
Instead of reversing the list just sort in reverse order:
for my $mods ( sort { $b cmp $a } keys %p_mod ) {
In modern versions of Perl that is a special case that is just as fast as the
default sort.
Wonderful! Thank you.
for my $position ( sort { $a <=> $b } keys %{ $p_mod{$mods}{fingers} } ) {
(Again, a special case for sort.)
<snip>
You are passing sort a list of keys from %{$p_mod{$mods}{fingers}} which is a
list of numbers and you are using those numbers (in $a and $b) where there are
only the keys 'fingers' and 'name'. You need to just sort the numbers in $a
and $b (see above.)
John
A beautiful, simple solution. Thank you very much.
Hardly
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>