I really need to order both the keys and one of the elements in the array
stored as a value in the hash, preferably sort first on the first element of
the array (my real application has four elements but the snippet I'm testing
with has a two-element array) and then sort secondly on the key.

So if the reference to the array stored as the hash value is called
"$value", then there are two elements, $$value[0] and $$value[1], that it
references, right?
I want to sort first on $$value[0], then on the key for HofA, and to then be
able to print a report that lists all the data I need, including the two
sorted values plus also the other, non-sorted values stored in the HofA
array.  That way the output is easier to read and summarize for the user.

Does this make sense?  I know how to sort by keys but sort first by the
value element and then by keys is my block...

-----Original Message-----
From: James Edward Gray II [mailto:[EMAIL PROTECTED] 
Sent: Thursday, February 19, 2004 11:37 AM
To: Smith Jeff D
Cc: '[EMAIL PROTECTED]'
Subject: Re: An Old Question on Sorting Hash of Arrays by Array element an d
th en by key


On Feb 19, 2004, at 10:25 AM, Smith Jeff D wrote:

> Thanks for the feedback--maybe I screwed up but what happens for me is
> that
> the ordered array (1) only lists the keys, not the array values the 
> hash key
> points to and (2) I still don't get an ordered list of the keys that 
> are put
> in the "ordered" array--it comes out un-ordered.

First, did you catch the later post that pointed out my mistake?  
Here's the corrected code:

my @ordered_keys = sort { $HofA{$a}[0] cmp $HofA{$b}[0]
                                           ||
                                           $a cmp $b } keys %HofA;


> I took your line and just added a for loop/print for the ordered array
> and
> got "red,yellow,blue, orange, violet, green" only  as the result.
>
> I must be dense but using just a Keys expression can't return the
> values,
> can it??--wouldn't it be better to do a while/each and get both key and
> value for the HofA somehow??

Yes, I only ordered the keys.  Sorry if I didn't make that clear.  
That's all you need though, right?  ;)

foreach (@ordered_keys) {
        print "$HofA{$_}[0], $_, $HofA{$_}[1]\n";
}

Hope that helps.

James

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to