Thanks, I must have missed it--I'll be getting back to it tomorrow morning
to see what I missed in the original response.  I thought I had run as
printed below.

Anyway, thank for the help...I think I may have braced when I should have
paren'd... It so simple when someone else does it first.... I was trying to
nest the sorts rather
than logically or'ing them together  and it wasn't working.

-----Original Message-----
From: James Edward Gray II [mailto:[EMAIL PROTECTED] 
Sent: Thursday, February 19, 2004 3:12 PM
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:48 AM, Smith Jeff D wrote:

> 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.

Are you playing with the code I'm posting?  :P

I know how you would like it sorted.  I did that.  (Actually, I believe 
I did miss the case insensitive part, but Rob has already fixed that.)

A Perl hash is an unordered structure.  However, if we put the keys in 
the order we want and then use those to access the values, we're good 
to go.  I posted a loop showing this in my last message.

I ordered the keys based on the first value and the key itself, just 
like you said.  In order to avoid more confusion though, here's a proof 
of concept:

#!/usr/bin/perl

use strict;
use warnings;

# create some data
my %HofA = ( orange     => ['ZZZ', 'ANDY'],
                         red    => ['AAA', 'AL'],
                         blue   => ['mmm','Betty'],
                         yellow => ['aaa', 'ZEUS'],
                         green  => ['DDD','Mary Joe'],
                         violet => ['MMM','Hugo'] );

# sort it
my @ordered_keys = sort { lc($HofA{$a}[0]) cmp lc($HofA{$b}[0])
                                                  ||
                                                  lc($a) cmp lc($b) } keys
%HofA;

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

__END__

You'll notice that the above is really just a summary of this thread.  
When I run it, I get:

AAA, red, AL
aaa, yellow, ZEUS
DDD, green, Mary Joe
mmm, blue, Betty
MMM, violet, Hugo
ZZZ, orange, ANDY

Which is the output you requested in your original message.

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