How can I Sort hash in numeric context

S

Sameer

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
 
V

Veli-Pekka Tätilä

Sameer said:
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.
 
T

Tad McClellan

Sameer said:
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;
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top