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

Re: Sorting my hashref by value for printing a popup_menu using CGI

by Roger (Parson)
on Jan 13, 2004 at 21:00 UTC ( [id://321110]=note: print w/replies, xml ) Need Help??


in reply to Sorting my hashref by value for printing a popup_menu using CGI

sub select_your_school { my $namesref = shift; # create a list of sorted hash keys my @menuitems = sort keys %$namesref; print $q->header, $q->start_html(-title=> 'Select Your School'), "Please select your school from the following list:", $q->start_form, $q->popup_menu( {-name => 'school_id', -values => \@menuitems , }), ....

Or you could do it in one step:
sub select_your_school { my $namesref = shift; print $q->header, $q->start_html(-title=> 'Select Your School'), "Please select your school from the following list:", $q->start_form, $q->popup_menu( { -name => 'school_id', -values => [ sort keys %$namesref ], }), .....

Various sorting options:
# Sort by alpha sort keys %$namesref # Sort by alpha reverse order sort { $b cmp $a } keys %$namesref # Sort by numeric value sort { $a <=> $b } keys %$namesref # etc...

Replies are listed 'Best First'.
Re: Re: Sorting my hashref by value for printing a popup_menu using CGI
by jerrygarciuh (Curate) on Jan 14, 2004 at 00:11 UTC
    Roger,
    Thanks for the reply but the array you create there will contain the sorted keys, not sorted values.
    TA
    jg
    _____________________________________________________
    "The man who grasps principles can successfully select his own methods.
    The man who tries methods, ignoring principles, is sure to have trouble.
    ~ Ralph Waldo Emerson
      The perl built-in function keys will retrieve a list of hash keys, and the built-in function values will retrieve a list of hash values.
      sort keys %$namesref # change keys to values to generate sorted list of values sort values %$namesref

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (6)
As of 2024-03-19 09:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found