|
From: Sameer on 25 Feb 2006 05:39 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
From: Veli-Pekka T?til? on 25 Feb 2006 06:58 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? (vtatila(a)mail.student.oulu.fi) Accessibility, game music, synthesizers and programming: http://www.student.oulu.fi/~vtatila/
From: Tad McClellan on 25 Feb 2006 09:38 Sameer <sameerkapadia(a)gmail.com> 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 tadmc(a)augustmail.com Perl programming Fort Worth, Texas
|
Pages: 1 Prev: FAQ 4.24 How do I reverse a string? Next: How to create a Wait Page? |