Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Re: More Sorted Business with Hashes

by Hrunting (Pilgrim)
on Dec 23, 2001 at 05:24 UTC ( [id://134010]=note: print w/replies, xml ) Need Help??


in reply to Re: More Sorted Business with Hashes
in thread More Sorted Business with Hashes

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.

Replies are listed 'Best First'.
Re: Re: Re: More Sorted Business with Hashes
by Hrunting (Pilgrim) on Dec 23, 2001 at 23:19 UTC
    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://134010]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (5)
As of 2024-04-24 13:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found