Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: More Sorted Business with Hashes

by Spenser (Friar)
on Dec 22, 2001 at 23:09 UTC ( [id://133981]=note: print w/replies, xml ) Need Help??


in reply to More Sorted Business with Hashes

Thanks for the feedback and ideas, but I couldn't get any of these ideas to work for me.  They do work if all I wanted to do is sort a hash by values.  But trying to drop such a sorted hash into the CGI directive, scrolling_list() seems to be too much for the module--it either fails or it still sorts according to key or it doesn't sort at all.

Anyway, I've worked around it. The content of my values already read, "$client_name - $clientid" with the key reading, "$clientid."  So I changed my scrolling_list to read simply like this:

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

This gives me the alphabetical sort based on the client names (e.g., values) that I need.  It does make the HTML option select value the same as the label (e.g., client name - client id)--granted, very messy.  But I put the following line in the next CGI script that parses out the data entered in the first script:

$acct_num = (split/ \- /, $acct_num)[-1];

This has solved my immediate problem.  However, if anyone can contribute more comments to my original question, I would glad to have them for my education and for those who might read this posting in the future.  Thanks.

Replies are listed 'Best First'.
Re: Re: More Sorted Business with Hashes
by Hrunting (Pilgrim) on Dec 23, 2001 at 05:24 UTC
    The CGI man page covers this:
    print $q->scolling_list( '-name' => 'acct_num', '-values' => [ sort values %clients ], '-labels' => \%clients '-default' => $default_value );
    It's under popup_menu(), but since they make the same HTML data structure with different attributes, it all works.
      Actually, to amend this, now that I've read it a few days later, you typically use a hash mapped from 'value' to 'text', so if your structure is like the one above, you may have something like:
      print $q->scrolling_list( '-name' => 'acct_num', '-values' => [ sort { $clients{$a} cmp $clients{$b} } keys %clients + ], '-labels' => \%clients, '-default' => $default_value );
      Or, if for some wacky reason, your values really are the values for your menu and the keys are the text labels:
      print $q->scrolling_list( '-name' => 'acct_nu', '-values' => [ sort values %clients ], '-labels' => { reverse %clients }, '-default' => $default_value );
      Typically, when using CGI, the first way is the way most commonly used. Most people don't keep around sorted copies of hashes. It defeats the purpose of a hash. Hashes inherently are unsorted because the algorithm they use to make their lookups fast stores them (internally) in an optimal configuration (which is almost never sorted).

      My fault for not reading the original scrolling_list() call correctly.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://133981]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (8)
As of 2024-04-23 14:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found