Sort Hash o Hash accordint to two keys

Discussion in 'Perl Misc' started by Malik Yousef, May 6, 2004.

  1. Malik Yousef

    Malik Yousef Guest

    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.
    Malik Yousef, May 6, 2004
    #1
    1. Advertising

  2. Malik Yousef

    Eric Bohlman Guest

    (Malik Yousef) wrote in
    news::

    > 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.
    Eric Bohlman, May 6, 2004
    #2
    1. Advertising

  3. Malik Yousef

    Uri Guttman Guest

    >>>>> "EB" == Eric Bohlman <> writes:

    EB> (Malik Yousef) wrote in
    EB> news::

    >> 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 ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
    Uri Guttman, May 6, 2004
    #3
  4. Malik Yousef

    Anno Siegel Guest

    Malik Yousef <> 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
    Anno Siegel, May 6, 2004
    #4
  5. 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?"
    Chris Mattern, May 6, 2004
    #5
  6. Malik Yousef

    Malik Yousef Guest

    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



    Uri Guttman <> wrote in message news:<>...
    > >>>>> "EB" == Eric Bohlman <> writes:

    >
    > EB> (Malik Yousef) wrote in
    > EB> news::
    >
    > >> 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
    Malik Yousef, May 6, 2004
    #6
  7. In article <c7crt4$1kr$-Berlin.DE>,
    -berlin.de (Anno Siegel) wrote:

    > Malik Yousef <> 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
    Richard Morse, May 6, 2004
    #7
  8. Malik Yousef

    Uri Guttman Guest

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

    >>>>> "MY" == Malik Yousef <> 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

    you post here, you read here.

    uri

    --
    Uri Guttman ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
    Uri Guttman, May 6, 2004
    #8
  9. Malik Yousef

    Malik Yousef Guest

    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 <> wrote in message news:<>...
    > first, don't top post. read the group guidelines which are posted
    > regularly.
    >
    > >>>>> "MY" == Malik Yousef <> 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
    >
    > you post here, you read here.
    >
    > uri
    Malik Yousef, May 7, 2004
    #9
  10. Malik Yousef

    Uri Guttman Guest

    >>>>> "MY" == Malik Yousef <> 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 ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
    Uri Guttman, May 7, 2004
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tommo
    Replies:
    2
    Views:
    3,480
    Gunnar Hjalmarsson
    Dec 15, 2003
  2. Navin
    Replies:
    1
    Views:
    685
    Ken Schaefer
    Sep 9, 2003
  3. Pål Bergström

    Sort hash with keys as integer

    Pål Bergström, Sep 25, 2010, in forum: Ruby
    Replies:
    3
    Views:
    147
    Robert Klemme
    Sep 26, 2010
  4. Milkweed
    Replies:
    6
    Views:
    337
    Milkweed
    Oct 30, 2003
  5. Malik Yousef

    Sort Hash o Hash accordint to two keys

    Malik Yousef, May 6, 2004, in forum: Perl Misc
    Replies:
    0
    Views:
    102
    Malik Yousef
    May 6, 2004
Loading...

Share This Page