Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Sorting my hashref by value for printing a popup_menu using CGI

by jerrygarciuh (Curate)
on Jan 13, 2004 at 20:34 UTC ( #321103=perlquestion: print w/ replies, xml ) Need Help??
jerrygarciuh has asked for the wisdom of the Perl Monks concerning the following question:

Honored monks,
I am passing a hashref to this sub and using it to make a select box. I would like to sort this hash by value but I don't understand how to apply the wisdom of the docs found here to making this popup_menu with CGI via a hashref. Perldoc shows how to make an array of keys but how to call it here?
TIA,
jg
sub select_your_school { my $namesref = shift; print $q->header,start_html(-title=> 'Select Your School'),"Please + select your school from the f +ollowing list:", start_form, popup_menu({ -name => 'school_id', -values => $namesref }), p( checkbox({ -name=>'not_list +ed', -value=>'true', -label=>' My schoo +l is not in this list.'}), ), submit("Submit"), end_form, end_html; exit; }

Comment on Sorting my hashref by value for printing a popup_menu using CGI
Download Code
Re: Sorting my hashref by value for printing a popup_menu using CGI
by Roger (Parson) on Jan 13, 2004 at 21:00 UTC
    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...
      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
Re: Sorting my hashref by value for printing a popup_menu using CGI
by injunjoel (Priest) on Jan 13, 2004 at 22:57 UTC
    Greetings all,
    sort keys question is a good node to check out for sorting your hash by value.
    Hope that helps.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://321103]
Approved by jdtoronto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (8)
As of 2015-07-04 14:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (60 votes), past polls