Hi Randy

You are absolutely right. I meant that the the perl subroutine does something like this:

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

Which means that it creates a new list @THRESHOLD into which the keys of the HASH %THRESHOLD are sorted and put. That's what you meant right? My earlier comment about creating a second hash was incorrect. Sorry about that.

Thanks for the advice :-) I appreciate it !
Nitin


On Mar 7, 2005, at 1:21 PM, Randy Harmon wrote:

You can't sort the keys of a hash - except so far as you can get the list of
keys in the hash, and you can sort a list. foreach( sort keys %foo ) does
exactly that - it's sorting a list of scalars that you've constructed, and
it's iterating through the list.


If you did #1, you wouldn't have a hash that has sorted keys. You'd just
have a second copy of the hash, whose keys would likely come out in the same
order as the first hash :)


If your subroutine needs to process the list of keys in sorted order, then
you can build that sorted list (in C, using a sort routine as mentioned) and
use it for indexing into the hash. If it's not important to process the
keys in sorted order, then just skip the sorting entirely.


Randy

-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]
Sent: Monday, March 07, 2005 10:13 AM
To: inline@perl.org
Subject: sort() function

Hi Guys

I am re-implementing a perl subroutine in C and this perl-subroutine
sorts the key of a hash given to it as input before using it.
This hash
usually hold about 3000-4000 keys.  So I am wondering what to do here:

1) Create another hash in perl-space and sort its keys using the perl
sort() function and then pass that hash as input
2) Write my own sort function or, even better, use one from a better
source like the Gnu Scientific Library or another third party library.

So, what I am basically asking is if it is worth replaceing
perl's sort
function with another one in inlined space or not ?

Thanks !
Nitin






Reply via email to