Sorting "string" numerical keys from a hash.

Discussion in 'Perl Misc' started by Gunnar Hjalmarsson, Sep 4, 2004.

  1. Edward Wijaya wrote:
    > I have a hash like this
    >
    > $VAR1 = {
    > '4' => 'ATTCAGAAACCTTctcCAGG',
    > '1' => 'CTCCcgcCGCGGTACAAAGG',
    > '3' => 'AAGGGTATGATGTCccgAAC',
    > '2' => 'ATtccCTCAGACTTCTGAGC',
    > '5' => 'ACTCGctcATCGCACATTTC'
    > };
    >
    >
    > I tried to do the sorting by the keys
    > using this:
    >
    > foreach my $key ( sort keys %seqs_hash )
    > {
    > #print ....
    > }


    That sorts lexically by key.

    > or with {$seqs_hash{$a} <=> $seqs_hash{$b}}


    That sorts numerically, but by value.

    > But these two attempt don't seem to work.
    > It treated the keys as a string instead
    > of numeric. I thought Perl always treated number
    > "as" number.
    >
    > How can I overcome this problem?


    By reading the docs for the sort function:

    perldoc -f sort

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Sep 4, 2004
    #1
    1. Advertising

  2. Gunnar Hjalmarsson

    Sam Holden Guest

    On Sun, 05 Sep 2004 13:47:42 +0800,
    Edward Wijaya <> wrote:
    > Hi,
    >
    > I have a hash like this
    >
    > $VAR1 = {
    > '4' => 'ATTCAGAAACCTTctcCAGG',
    > '1' => 'CTCCcgcCGCGGTACAAAGG',
    > '3' => 'AAGGGTATGATGTCccgAAC',
    > '2' => 'ATtccCTCAGACTTCTGAGC',
    > '5' => 'ACTCGctcATCGCACATTTC'
    > };
    >
    >
    > I tried to do the sorting by the keys
    > using this:
    >
    > foreach my $key ( sort keys %seqs_hash )
    > {
    > #print ....
    > }


    That will sort the keys alphabetically.

    >
    > or with {$seqs_hash{$a} <=> $seqs_hash{$b}}


    That will sort based upon the values, numerically.

    Since the keys are numbers, and the values are strings, both
    of those attempts are doomed to failure.


    >
    > But these two attempt don't seem to work.
    > It treated the keys as a string instead
    > of numeric. I thought Perl always treated number
    > "as" number.


    Nope. Perl treats things as numbers if they are used as numbers,
    and strings if they are used as strings.

    >
    > How can I overcome this problem?


    Merge the two attempts above to sort the keys numerically.

    --
    Sam Holden
    Sam Holden, Sep 4, 2004
    #2
    1. Advertising

  3. Hi,

    I have a hash like this

    $VAR1 = {
    '4' => 'ATTCAGAAACCTTctcCAGG',
    '1' => 'CTCCcgcCGCGGTACAAAGG',
    '3' => 'AAGGGTATGATGTCccgAAC',
    '2' => 'ATtccCTCAGACTTCTGAGC',
    '5' => 'ACTCGctcATCGCACATTTC'
    };


    I tried to do the sorting by the keys
    using this:

    foreach my $key ( sort keys %seqs_hash )
    {
    #print ....
    }

    or with {$seqs_hash{$a} <=> $seqs_hash{$b}}

    But these two attempt don't seem to work.
    It treated the keys as a string instead
    of numeric. I thought Perl always treated number
    "as" number.

    How can I overcome this problem?

    Hope to hear from you again.
    Thanks so much for your time.

    Regards,
    Edward WIJAYA
    SINGAPORE
    Edward Wijaya, Sep 5, 2004
    #3
  4. Gunnar Hjalmarsson

    Joe Smith Guest

    Edward Wijaya wrote:

    > I tried to do the sorting by the keys using this:
    > foreach my $key ( sort keys %seqs_hash )
    >
    > or with {$seqs_hash{$a} <=> $seqs_hash{$b}}


    You said you tried to sort by keys, and then write something that
    sorts by values. Did you really mean to change your mind like that?

    foreach my $key ( sort {$a <=> $b} keys %seqs_hash) { ... }

    -Joe
    Joe Smith, Sep 6, 2004
    #4
    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. rp
    Replies:
    1
    Views:
    492
    red floyd
    Nov 10, 2011
  2. Xeno Campanoli
    Replies:
    16
    Views:
    253
    Martin DeMello
    Aug 25, 2005
  3. Alex Fenton

    Hash#values and Hash#keys order

    Alex Fenton, Apr 7, 2006, in forum: Ruby
    Replies:
    1
    Views:
    128
    George Ogata
    Apr 15, 2006
  4. Aldric Giacomoni

    Sorting keys of hash based on value

    Aldric Giacomoni, Mar 22, 2010, in forum: Ruby
    Replies:
    4
    Views:
    134
    Robert Klemme
    Mar 22, 2010
  5. anthony
    Replies:
    5
    Views:
    105
    J├╝rgen Exner
    Dec 9, 2005
Loading...

Share This Page