how to sort a hash of arrays?

J

Jerry Preston

I know the following works:

if( @{ $dat{ $ST }} ) {
@dat_ = @{ $dat{ $ST }};
foreach $j ( @dat ) {
....
}
}
I am trying to figure out how to do in the following format of which do not
work:

foreach $j ( sort { @{ $wafers{ $ST }{ $a } } cmp @{ $wafers{ $ST }{
$b } } } @{ $wafers{ $ST }} ) {
or
foreach $j ( sort { @{ $wafers{ $a } } cmp @{ $wafers{ $b } } } keys
%wafers ) {
or
if( @{ $dat{ $ST }} ) {
$j= [ sort { $a cmp $b } @{ $j } ];

Any ideas?

Thanks,

Jerry
 
A

Anno Siegel

Jerry Preston said:
I know the following works:

if( @{ $dat{ $ST }} ) {
@dat_ = @{ $dat{ $ST }};
foreach $j ( @dat ) {
....
}
}

Works? Whatever you mean with that, you are using "dat" once as a
hash and once as an array. Your array is "@dat_", so no, it won't
even compile.
I am trying to figure out how to do in the following format of which do not
work:

"Does not work" is no valid error description, especially since
you have never said what "work" would mean.
foreach $j ( sort { @{ $wafers{ $ST }{ $a } } cmp @{ $wafers{ $ST }{
$b } } } @{ $wafers{ $ST }} ) {
or
foreach $j ( sort { @{ $wafers{ $a } } cmp @{ $wafers{ $b } } } keys
%wafers ) {
or
if( @{ $dat{ $ST }} ) {
$j= [ sort { $a cmp $b } @{ $j } ];

Any ideas?

No. Say what you want to achieve and we may have suggestions.

Throwing bits of code around without saying what they are supposed
to do is just as bad as a verbal problem description without any code.

Anno
 
J

Jürgen Exner

Jerry said:
I know the following works:

By some private definition of "works" which you didn't share with us.

[code snipped]
I am trying to figure out how to do in the following format of which
do not work:

By some private definition of "do not work" which you didn't share with us.

[code snipped]
Any ideas?

Yes. Please let us know what you expect the code to do ("it works") versus
what you are actually observing ("does not work"). We are not mind readers,
therefore we cannot know what your were thinking when you wrote "works"
(although some people are close).

As for "works" in combinatino with your subject line: this is akin to a
perpetuum mobile. A hash by definition does not have a (useful) order,
therefore you cannot sort it. You can probably sort the keys or sort the
values, but there is no way to sort the hash itself.

There is the Tie::IxHash module, which emulates order on hashes. Please see
Google about earlier postings on this subject.

Oh, and before I forget: this _Q_uestion is _A_sked _F_requenty. Please
check the FAQ (perldoc -q hash) beforehand next time:
How do I sort a hash (optionally by value instead of key)?
How can I always keep my hash sorted?


jue
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top