Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.

Re: Re: Re: More Sorted Business with Hashes

by Hrunting (Pilgrim)
on Dec 23, 2001 at 23:19 UTC ( [id://134084] : note . print w/replies, xml ) Need Help??

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

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.