Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
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.