Sort Hash o Hash accordint to two keys

M

Malik Yousef

Hi
I have the fellwoing hash structure:
%allresults{hdrnam}{WinPosition}


First i would like to sort the hash according to the key "hdrname" and
then to sort according to the WinPosition which is with numeric value.
 
E

Eric Bohlman

(e-mail address removed) (Malik Yousef) wrote in
Hi
I have the fellwoing hash structure:
%allresults{hdrnam}{WinPosition}


First i would like to sort the hash according to the key "hdrname" and
then to sort according to the WinPosition which is with numeric value.

"perldoc -q sort" will bring up a very helpful tutorial entitled "How do I
sort an array by (anything)?" Now of course you say you want to sort a
*hash* rather than an array, but that's not really possible. You probably
want to build an array of indices into the hash and then sort that array;
the code in the FAQ will help you with that.
 
U

Uri Guttman

EB> (e-mail address removed) (Malik Yousef) wrote in
EB>
EB> "perldoc -q sort" will bring up a very helpful tutorial entitled
EB> "How do I sort an array by (anything)?" Now of course you say you
EB> want to sort a *hash* rather than an array, but that's not really
EB> possible. You probably want to build an array of indices into the
EB> hash and then sort that array; the code in the FAQ will help you
EB> with that.

huh?? there is even an faq on how to sort a hash (by key or value).

uri
 
A

Anno Siegel

Malik Yousef said:
Hi
I have the fellwoing hash structure:
%allresults{hdrnam}{WinPosition}

That's not Perl.
First i would like to sort the hash according to the key "hdrname" and
then to sort according to the WinPosition which is with numeric value.

Your specification makes no sense, specifically the part beginning
with "and then...".

"Sorting a hash" is a loose way of saying "sorting the keys of a hash".
So you want to sort the keys, alphabetically presumably. But hash
keys are unique, so the sorting order is completely specified. There
is no way to bring in a secondary sort order.

Anno
 
C

Chris Mattern

Malik said:
Hi
I have the fellwoing hash structure:
%allresults{hdrnam}{WinPosition}


First i would like to sort the hash according to the key "hdrname" and
then to sort according to the WinPosition which is with numeric value.

You can't sort a hash; hashes are unordered.
--
Christopher Mattern

"Which one you figure tracked us?"
"The ugly one, sir."
"...Could you be more specific?"
 
M

Malik Yousef

Let me make it simple
I have the hash keys with two information, the name and the window
position separated by <->, for example:
Bc007713_220356885-220369973_intron15_hsa-mir-153-1<->1492
Bc007713_220356885-220369973_intron15_hsa-mir-153-1<->1531
Bc007713_220356885-220369973_intron15_hsa-mir-153-1<->1795
Bc007713_220356885-220369973_intron15_hsa-mir-153-1<->23
Bc007713_220356885-220369973_intron15_hsa-mir-153-1<->2918
Bc007713_220356885-220369973_intron15_hsa-mir-153-1<->2921
Bc007713_220356885-220369973_intron15_hsa-mir-153-1<->2925
Bc007713_220356885-220369973_intron15_hsa-mir-153-1<->3304
Bc007713_220356885-220369973_intron15_hsa-mir-153-1<->3305

I want to sort this keys according to the name and the window
position, so
*<->23 should be as the first element, so how could one sort according
to the first part ($1)<->($2) and the second part at the same time.
Please send your reply also to (e-mail address removed)
 
R

Richard Morse

That's not Perl.


Your specification makes no sense, specifically the part beginning
with "and then...".

"Sorting a hash" is a loose way of saying "sorting the keys of a hash".
So you want to sort the keys, alphabetically presumably. But hash
keys are unique, so the sorting order is completely specified. There
is no way to bring in a secondary sort order.

Perhaps the OP wants something like this:

#!/usr/bin/perl
use strict;
use warnings;

my %allresults = ( ... );

foreach my $header_name (sort keys %allresults) {
foreach my $win_position (sort keys %{$allresults{$header_name}}) {
print "${header_name}, ${win_position}\n";
}
}

__END__

Ricky
 
U

Uri Guttman

first, don't top post. read the group guidelines which are posted
regularly.

MY> Let me make it simple
MY> I have the hash keys with two information, the name and the window
MY> position separated by <->, for example:

what is a window position? this is already more complex than simple.

MY> Bc007713_220356885-220369973_intron15_hsa-mir-153-1<->1492
MY> Bc007713_220356885-220369973_intron15_hsa-mir-153-1<->1531
MY> Bc007713_220356885-220369973_intron15_hsa-mir-153-1<->1795
MY> Bc007713_220356885-220369973_intron15_hsa-mir-153-1<->23
MY> Bc007713_220356885-220369973_intron15_hsa-mir-153-1<->2918
MY> Bc007713_220356885-220369973_intron15_hsa-mir-153-1<->2921
MY> Bc007713_220356885-220369973_intron15_hsa-mir-153-1<->2925
MY> Bc007713_220356885-220369973_intron15_hsa-mir-153-1<->3304
MY> Bc007713_220356885-220369973_intron15_hsa-mir-153-1<->3305

MY> I want to sort this keys according to the name and the window
MY> position, so
MY> *<->23 should be as the first element, so how could one sort according
MY> to the first part ($1)<->($2) and the second part at the same time.

you don't have any hash i can see there. you have a list of
values. again, i refer you to the FAQ which answers this. read perldoc
-q sort and write some code and come back if you need more help. post
just the short code sample you wrote.

MY> Please send your reply also to (e-mail address removed)

you post here, you read here.

uri
 
M

Malik Yousef

Here the code i'm using now:

@pos = sort {
$allresults{$a}{HDR} cmp $allresults{$b}{HDR}
|| $allresults{$a}{WinStart} <=>
$allresults{$b}{WinStart}
# $allresults{$a}{WinStart} <=>
$allresults{$b}{WinStart}
} keys %allresults;


But the results that i'm getting are partioally sorted according to
the used commands.
 
U

Uri Guttman

please stop top posting. look at the difference with how i reply to your
posts. read the regularly posted group guidelines.


MY> Here the code i'm using now:
MY> @pos = sort {
MY> $allresults{$a}{HDR} cmp $allresults{$b}{HDR}
MY> || $allresults{$a}{WinStart} <=>
MY> $allresults{$b}{WinStart}
MY> # $allresults{$a}{WinStart} <=>
^
what is that doing there?

i can't tell what you actually are running because of the odd line
wrap.

MY> $allresults{$b}{WinStart}
MY> } keys %allresults;


MY> But the results that i'm getting are partioally sorted according to
MY> the used commands.

and what is in %allresults? you don't show how you stuff it and that can
be half the work.

uri
 

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

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top