FAQ
I am trying to sort a hash of arrays that is similar to the example below.
I have a hash of arrays that I want to sort, first by the first element of
the array, then by the key to the hash and don't care about other elements
of the array (for sorting and without regard to case.

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

comes out like this:

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

The following snippet doesn't sort the stuff the way I want--need some
help....

Thanks in advance.

Jeff Smith

# BEGINNING SNIPPET:
use strict;
use warnings;
my %testhash = ();
(my $value, my $key) = '';
%testhash = (orange=>['ZZZ', 'ANDY'],
red=>['AAA', 'AL'],
blue=>['MMM','Betty'],
yellow=>['AAA', 'ZEUS'],
green=>['DDD','Mary Joe']
violet=>['MMM','Hugo']
);

while (($key, $value) = each %testhash) {
my @array = ($value->[0], $key, $value->[1]);
my @newarray = sort @array;
print "array is @newarray\n";
}


####End of snippet

Search Discussions

  • James Edward Gray II at Feb 18, 2004 at 11:50 pm

    On Feb 18, 2004, at 1:58 PM, Smith Jeff D wrote:

    I am trying to sort a hash of arrays that is similar to the example
    below.
    I have a hash of arrays that I want to sort, first by the first
    element of
    the array, then by the key to the hash and don't care about other
    elements
    of the array (for sorting and without regard to case.

    %HofA = (orange=>['ZZZ', 'ANDY'],
    red=>['AAA', 'AL'],
    blue=>['mmm','Betty'],
    yellow=>['aaa', 'ZEUS'],
    green=>['DDD','Mary Joe']
    violet=>['MMM','Hugo']
    );
    my @ordered_keys = sort { $HofA{$a}[0] cmp $HofA{$b}[1]
    $a cmp $b } keys %HofA;

    I believe that will do it. See if that gets you going.

    James
  • Mark LoBue at Feb 19, 2004 at 12:16 am

    At 03:49 PM 2/18/2004, James Edward Gray II wrote:
    On Feb 18, 2004, at 1:58 PM, Smith Jeff D wrote:

    I am trying to sort a hash of arrays that is similar to the example below.
    I have a hash of arrays that I want to sort, first by the first element of
    the array, then by the key to the hash and don't care about other elements
    of the array (for sorting and without regard to case.

    %HofA = (orange=>['ZZZ', 'ANDY'],
    red=>['AAA', 'AL'],
    blue=>['mmm','Betty'],
    yellow=>['aaa', 'ZEUS'],
    green=>['DDD','Mary Joe']
    violet=>['MMM','Hugo']
    );
    my @ordered_keys = sort { $HofA{$a}[0] cmp $HofA{$b}[1]
    $a cmp $b } keys %HofA;
    Ok, I'll bite, why this:
    my @ordered_keys = sort { $HofA{$a}[0] cmp $HofA{$b}[1]
    and not this:
    my @ordered_keys = sort { $HofA{$a}[0] cmp $HofA{$b}[0]

    I would have guessed you want the same array element in the 2 comparison values.

    -Mark
  • James Edward Gray II at Feb 19, 2004 at 12:21 am

    On Feb 18, 2004, at 6:16 PM, Mark LoBue wrote:
    At 03:49 PM 2/18/2004, James Edward Gray II wrote:
    On Feb 18, 2004, at 1:58 PM, Smith Jeff D wrote:

    I am trying to sort a hash of arrays that is similar to the example
    below.
    I have a hash of arrays that I want to sort, first by the first
    element of
    the array, then by the key to the hash and don't care about other
    elements
    of the array (for sorting and without regard to case.

    %HofA = (orange=>['ZZZ', 'ANDY'],
    red=>['AAA', 'AL'],
    blue=>['mmm','Betty'],
    yellow=>['aaa', 'ZEUS'],
    green=>['DDD','Mary Joe']
    violet=>['MMM','Hugo']
    );
    my @ordered_keys = sort { $HofA{$a}[0] cmp $HofA{$b}[1]
    $a cmp $b } keys %HofA;
    Ok, I'll bite, why this:
    my @ordered_keys = sort { $HofA{$a}[0] cmp $HofA{$b}[1]
    and not this:
    my @ordered_keys = sort { $HofA{$a}[0] cmp $HofA{$b}[0]

    I would have guessed you want the same array element in the 2
    comparison values.
    You're exactly right. Egad. Long day. My bad.

    James

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupbeginners @
categoriesperl
postedFeb 18, '04 at 7:59p
activeFeb 19, '04 at 12:21a
posts4
users3
websiteperl.org

People

Translate

site design / logo © 2022 Grokbase