Click here to get back home

sort on multiple hash values

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
sort on multiple hash values Zhiliang Hu 10-08-2008
Get Chitika Premium
Posted by Zhiliang Hu on October 8, 2008, 1:36 pm
Please log in for more thread options


I have a hash with multiple values, like in:

Marty => 32877::2
Bart => 14857::2
Torq => 65221::1
etc

and I wish to sort on. say, first field of the hash value:

foreach $key (sort mysort keys %hasha ) { ... }

sub mysort {
map { $_->[0] }
sort { $a->[0] cmp $b->[0] }
map { [ $_, split(/::/,[0] ] }
}

but confused as how to 'map' the elements... need help -- Thanks in
advance!

Posted by Jürgen Exner on October 8, 2008, 2:04 pm
Please log in for more thread options


>I have a hash with multiple values, like in:
>
> Marty => 32877::2
> Bart => 14857::2
> Torq => 65221::1
> etc
>
>and I wish to sort on. say, first field of the hash value:

You don't because hashes by definition do not have a sequence and
therefore cannot be sorted.

What _do_ you want to sort? An array of the keys of the hash? An arrray
of the values of the hash? Something totally different?

See also the FAQ: "How do I sort a hash[...]?"

jue

Posted by Peter J. Holzer on October 11, 2008, 3:43 am
Please log in for more thread options


>>I have a hash with multiple values, like in:
>>
>> Marty => 32877::2
>> Bart => 14857::2
>> Torq => 65221::1
>> etc
>>
>>and I wish to sort on. say, first field of the hash value:
>
> You don't because hashes by definition do not have a sequence and
> therefore cannot be sorted.

Jürgen, can you please try to be little less literal? I see no
indication that the OP wanted to create a hash with an intrinsic sort
order.


> What _do_ you want to sort? An array of the keys of the hash? An arrray
> of the values of the hash?

One of those two, almost certainly. It doesn't really matter, since the
technique is the same, you just write

sort { cmp_func($hash, $hash) } keys %hash
in one case and
sort { cmp_func($a, $b) } values %hash
in the other (the OP already stated that the sort order depends only on
the value, not the key).


> Something totally different?

Unlikely.


> See also the FAQ: "How do I sort a hash[...]?"

I don't see you berating Brian that he can't sort a hash ...

        hp

Posted by Jürgen Exner on October 11, 2008, 4:50 am
Please log in for more thread options


>>>I have a hash with multiple values, like in:
>>>
>>> Marty => 32877::2
>>> Bart => 14857::2
>>> Torq => 65221::1
>>> etc
>>>
>>>and I wish to sort on. say, first field of the hash value:
>>
>> You don't because hashes by definition do not have a sequence and
>> therefore cannot be sorted.
>
>Jürgen, can you please try to be little less literal? I see no
>indication that the OP wanted to create a hash with an intrinsic sort
>order.

I guess that's my pet peeve. If people would stop talking about sorting
a hash and instead spend just a minute thinking about what they _REALLY_
want to do, then the solution would be obvious, please see below.

[...]

> sort { cmp_func($hash, $hash) } keys %hash

This is I want to sort the keys of the hash by their associated values,
using some criteria.

>in one case and
> sort { cmp_func($a, $b) } values %hash

And this is I want to sort the values of the hash by their own values,
using some criteria.

Both follow the standard pattern "I want to sort X by criteria Y". The
solution will jump right at you once you recognize that sort() takes two
parameters: the compare function and the list(!) of data to be sorted.
So first get that list (no, it is not the hash!), and then based on the
elements in that list define your compare function.

Once you forget the idea about sorting a hash and realize that actually
you are sorting a list and that you have to create that list first, then
the problem looses the 'rocket science complexity' and becomes trivial.

Unfortunately most people keep insisting on thinking in terms of sorting
a hash.... :-((

jue

Posted by John W. Krahn on October 8, 2008, 2:12 pm
Please log in for more thread options


Zhiliang Hu wrote:
> I have a hash with multiple values, like in:
>
> Marty => 32877::2
> Bart => 14857::2
> Torq => 65221::1
> etc
>
> and I wish to sort on. say, first field of the hash value:

Have you thought about using a Hash of Arrays instead?

my %hasha = (
Marty => [ 32877, 2 ],
Bart => [ 14857, 2 ],
Torq => [ 65221, 1 ],
);


> foreach $key (sort mysort keys %hasha ) { ... }

Why are you are supplying sort with a subroutine that has sort in it?
Did you want to sort on the keys as well as the values?

foreach my $key ( mysort keys %hasha ) { ... }


> sub mysort {
> map { $_->[0] }
> sort { $a->[0] cmp $b->[0] }
> map { [ $_, split(/::/,[0] ] }

You are supplying split() with an anonymous array which translates to a
number that has no ':' characters in it. The mysort subroutine is
passed a list of keys from the %hasha hash in the @_ array but you are
not accessing that list, but even if you were, to get the value from
that key you need to get it from the hash as $hasha. You are
comparing what would be the keys of the hash but you said you wanted to
compare the values.


> }
>
> but confused as how to 'map' the elements... need help -- Thanks in
> advance!

sub mysort {
map $_->[ 0 ],
sort { $a->[ 1 ] <=> $b->[ 1 ] }
map [ $_, ( split /::/, $hasha{ $_ } )[ 0 ] ],
@_
}



John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall

Similar ThreadsPosted
Sort on multiple values January 26, 2007, 4:18 am
assigning multiple hash values to multiple variables May 2, 2006, 7:55 pm
sort hash values does not work September 14, 2006, 11:15 pm
Comparing values of multiple hash keys July 26, 2006, 2:50 am
How to sort by multi values? May 24, 2007, 2:21 am
Sort 2-d array using multiple columns July 20, 2005, 4:13 pm
Sort by hash vaule, an array of hash references October 6, 2005, 12:52 pm
passing multiple values into an argument as an array ? September 22, 2006, 11:43 pm
copying values from a hash into CGI.pm via tied hash reference December 27, 2004, 7:02 am
Module for getting values from "flattened" hash or "recursed" hash...? March 8, 2005, 7:05 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap