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