Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Sort Hash o Hash accordint to two keys

Reply
Thread Tools

Sort Hash o Hash accordint to two keys

 
 
Malik Yousef
Guest
Posts: n/a
 
      05-06-2004
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.
 
Reply With Quote
 
 
 
 
Eric Bohlman
Guest
Posts: n/a
 
      05-06-2004
http://www.velocityreviews.com/forums/(E-Mail Removed) (Malik Yousef) wrote in
news:(E-Mail Removed) om:

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

 
Reply With Quote
 
 
 
 
Uri Guttman
Guest
Posts: n/a
 
      05-06-2004
>>>>> "EB" == Eric Bohlman <(E-Mail Removed)> writes:

EB> (E-Mail Removed) (Malik Yousef) wrote in
EB> news:(E-Mail Removed) om:

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


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

--
Uri Guttman ------ (E-Mail Removed) -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      05-06-2004
Malik Yousef <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> 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
 
Reply With Quote
 
Chris Mattern
Guest
Posts: n/a
 
      05-06-2004
Malik Yousef wrote:

> 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?"
 
Reply With Quote
 
Malik Yousef
Guest
Posts: n/a
 
      05-06-2004
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 Removed)



Uri Guttman <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> >>>>> "EB" == Eric Bohlman <(E-Mail Removed)> writes:

>
> EB> (E-Mail Removed) (Malik Yousef) wrote in
> EB> news:(E-Mail Removed) om:
>
> >> 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.

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

 
Reply With Quote
 
Richard Morse
Guest
Posts: n/a
 
      05-06-2004
In article <c7crt4$1kr$(E-Mail Removed)-Berlin.DE>,
http://www.velocityreviews.com/forums/(E-Mail Removed)-berlin.de (Anno Siegel) wrote:

> Malik Yousef <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> > 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.


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

--
Pukku
 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      05-06-2004

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

>>>>> "MY" == Malik Yousef <(E-Mail Removed)> writes:


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 Removed)

you post here, you read here.

uri

--
Uri Guttman ------ (E-Mail Removed) -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
 
Reply With Quote
 
Malik Yousef
Guest
Posts: n/a
 
      05-07-2004
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.

Uri Guttman <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> first, don't top post. read the group guidelines which are posted
> regularly.
>
> >>>>> "MY" == Malik Yousef <(E-Mail Removed)> writes:

>
> 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 Removed)
>
> you post here, you read here.
>
> uri

 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      05-07-2004
>>>>> "MY" == Malik Yousef <(E-Mail Removed)> writes:

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

--
Uri Guttman ------ (E-Mail Removed) -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
hash of hash of hash of hash in c++ rp C++ 1 11-10-2011 04:45 PM
Sort hash with keys as integer Pål Bergström Ruby 3 09-26-2010 08:02 PM
Sort Hash o Hash accordint to two keys Malik Yousef Perl Misc 0 05-06-2004 01:42 AM
How can you sort non-unique 'numeric keys' in a Perl hash Tommo Perl 2 12-15-2003 11:05 AM
Ado sort error-Ado Sort -Relate, Compute By, or Sort operations cannot be done on column(s) whose key length is unknown or exceeds 10 KB. Navin ASP General 1 09-09-2003 07:16 AM



Advertisments