Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

comment on

( [id://3333] : superdoc . print w/replies, xml ) Need Help??

Is there a way to sort a hash by values and leave it in a hash?  I found postings regarding sorting by keys:

sort keys %hash;

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:

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

And I found cwest's postings regarding his Tie::SortHash module.  From that I produced this:

tie %clients, 'Tie::SortHash', \%clients, $sortblock; my $sortblock = q(my $c = (split /\s+/, $a)[1]; my $d = (split /\s+/, $b)[1]; $c cmp $d || $clients{$a} <=> $clients{$b});

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:

print $q->scrolling_list(-name=>'acct_num', -values=> [keys %clients], -labels=> \%clients);

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.


In reply to More Sorted Business with Hashes by Spenser

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.