The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

import-bot (20211) [Avatar] Offline
#1
Re: Numeric sorting of Hash of Array
[Originally posted by hrao]

> Hmm.. The forum ate backslashes before @arry1, @arry2 and @arry3.
> So, I guess you had them too

No, actually, I did not have the backslashes for the arrays in my original
mail, but I do realize that you can only store references to arrays. Thanks
for pointing this out.
Also appreciate your help in this matter! The perl community is indeed great!
import-bot (20211) [Avatar] Offline
#2
[Originally posted by hrao]

I have a data structure (hash of arrays) as given below:

@arry1 = (3, 8, 9);
@arry2 = (-3, 4, 1);
@arry3 = (5, -7, 2);
%HoA = (
sig1 => @arry1,
sig2 => @arry2,
sig3 => @arry3,
);

Question is: How do I sort this data structure by first element of each array
numerically in an ascending order? How do I print it out?

----------------------------------------------------------
My intended output is:

sig2 :: -3, 4, 1
sig1 :: 3, 8, 9
sig3 :: 5, -7, 2
----------------------------------------------------------

Thank you!
import-bot (20211) [Avatar] Offline
#3
Re: Numeric sorting of Hash of Array
[Originally posted by genie]


Hi,
You should store not a complete arrays in the hash but a references to them.
Note the differences below :

@arry1 = (3, 8, 9);
@arry2 = (-3, 4, 1);
@arry3 = (5, -7, 2);
%HoA = (
sig1 => @arry1,
sig2 => @arry2,
sig3 => @arry3,
);

And here's the code that does what you need :

for my $key ( sort { $HoA{ $a }[0] <=> $HoA{ $b }[0] } keys %HoA ){
print "$key :: @{ $HoA{ $key }}
";
}
import-bot (20211) [Avatar] Offline
#4
Re: Numeric sorting of Hash of Array
[Originally posted by genie]

Hmm.. The forum ate backslashes before @arry1, @arry2 and @arry3.
So, I guess you had them too
import-bot (20211) [Avatar] Offline
#5
Re: Numeric sorting of Hash of Array
[Originally posted by dave]

Thanks for posting such a useful answer whilst I was too busy at The Perl
Conference to read the message board.

Dave...