How can I Sort hash in numeric context

Discussion in 'Perl Misc' started by Sameer, Feb 25, 2006.

  1. Sameer

    Sameer Guest

    Hi,
    I wanted to sort hash on keys, So I did the following
    #!usr/bin/perl -w
    use strict;
    my @interval;
    my
    @midpoint=(45419294,45419807,45420010,45420487,45421043,45421491,45421781,45422084,45422591,45422955,45423008);
    my $var;

    for ($var=0;$var<@midpoint-1;$var++){
    my $x=($midpoint[$var+1]-$midpoint[$var]);
    push(@interval,int($x));
    }

    print "@interval\n";

    my $u=0;
    my %inv;
    my $l;
    foreach $l (@interval){
    $inv{ ($l) } = ($u);
    $u++;
    }

    my @g=sort (keys %inv);
    print "@g\n";

    However I am getting results as :

    No Sort : 513 203 477 556 448 290 303 507 364 53
    Sorted : 203 290 303 364 448 477 507 513 53 556

    How can I get sort hash on key so that I get result as
    53 203 290 303 364 448 477 507 513 556
    Numeric Sort?

    Thanks in advance,
    Sameer
    Sameer, Feb 25, 2006
    #1
    1. Advertising

  2. Sameer wrote:
    > I wanted to sort hash on keys, So I did the following

    <code snipped>

    No, hashes don't work like this in Perl or in most other languages. They are
    essentially unordered and when you refer to the hash keys you only get a
    copy of them so re-sorting them has no effect at all. Try typing in
    perldoc -f sort and perldoc -f keys for some hints.

    In stead, you'll have to ask the hash for its keys and sort the keys. The
    sort function can sort any list not just an array. You can then do something
    to the list returned by sort such as walking it through like this:

    Code:
    # untested.
    foreach my $key (sort keys {$a <=> $b } %hash)
    {
    # some code here.
    } # for
    End code.

    The block in the sub-routine call is just an anonymous, i.e. unnamed,
    callback sorting subroutine. See sort and perldoc perlsub for details,
    again. The reason why I put it in is that sort normaly sorts everything
    ASCIIbetically rather than numerically.

    If you'd like to maintain the order of hash keys, you can keep a sorted copy
    of the keys and then return it to the caller for iteration. There are also
    modules for hashes that maintain the insertion order, that is the order in
    which you put in the keys and values. PHP always does this for some reason,
    too. There might be automagically sorting hashes as well or you could store
    copies of keys in a priority queue and so on. If you are using Active State
    Perl try saying search hash in the ppm package manager (type in ppm to start
    it). IF you hav some other Perl distro, you can try searching CPAN which is
    a huge Perl module site. URL:

    http://www.cpan.org/

    Hope this helps.

    PS: You might want to get a good Perl book as well. I recommend the latest
    4th edition of Learning Perl.

    --
    With kind regards Veli-Pekka Tätilä ()
    Accessibility, game music, synthesizers and programming:
    http://www.student.oulu.fi/~vtatila/
    Veli-Pekka Tätilä, Feb 25, 2006
    #2
    1. Advertising

  3. Sameer <> wrote:

    > I wanted to sort hash on keys,


    > However I am getting results as :
    >
    > No Sort : 513 203 477 556 448 290 303 507 364 53
    > Sorted : 203 290 303 364 448 477 507 513 53 556
    >
    > How can I get sort hash on key so that I get result as
    > 53 203 290 303 364 448 477 507 513 556
    > Numeric Sort?



    The way it says to in the Perl FAQ of course.

    perldoc -q sort

    How do I sort an array by (anything)?

    Which answers your question within the first 3 lines of the given answer.

    You are expected to check the Perl FAQ *before* posting
    to the Perl newsgroup you know.

    my @g = sort {$a <=> $b} keys %inv;


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Feb 25, 2006
    #3
    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. Replies:
    5
    Views:
    931
    X-Centric
    Jun 30, 2005
  3. darrel
    Replies:
    4
    Views:
    811
    darrel
    Jul 19, 2007
  4. Navin
    Replies:
    1
    Views:
    685
    Ken Schaefer
    Sep 9, 2003
  5. rp
    Replies:
    1
    Views:
    513
    red floyd
    Nov 10, 2011
Loading...

Share This Page