|Don't ask to ask, just ask|
More Sorted Business with Hashesby Spenser (Friar)
|on Dec 22, 2001 at 05:12 UTC||Need Help??|
Spenser has asked for the wisdom of the Perl Monks concerning the following question:
Is there a way to sort a hash by values and leave it in a hash? I found postings regarding sorting by keys:
I also spotted a posting on a way to sort a hash by values to put it in an array, but not leaving it in a hash:
But this module still only seems to sort by keys and not values. Maybe there's another way to use this module to get a sort by values. If anyone knows, please let me know how to do it. However, maybe I'm trying to make a hash act like an array--that certainly seems to be where I'm going wrong--when an array will do me just fine. Let me explain what I'm trying to do and why I'm trying to stay with a hash.
I'm trying to pull information out of a mySQL database which includes two fields: client id's and client names. I've pulled that off without much trouble and fed the data into a hash. From there, I want to display the data in a scrolling list in a web page using the CGI module. This is the code I used for that:
Again, if I put a "sort" in front of "keys" on the second line, it will sort by the client identification number. But I need to sort by the values since the users don't know our client id's. I thought of swapping keys with values when creating the hash, but I need to pass the key to the next web page and the values are fraught with spaces and other undesirable characters (much like our clients). A simple solution to my problem would be to be able to sort the hash into an array (like the second bit of code above) and then plop that into the labels field of the scrolling_list's properties. But, I can't seem to get the scrolling_list command to take an array. Is there a way for one to give it an array instead of a hash?
Any ideas will be appreciated.