Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

How do I order a hash for use as a %label list in cgi.pm?

by filmo (Scribe)
on Mar 21, 2001 at 10:58 UTC ( [id://65977]=perlquestion: print w/replies, xml ) Need Help??

filmo has asked for the wisdom of the Perl Monks concerning the following question:

I'm passing a %hash of values/labels to cgi->popup_menu and scrolling_list. Since %hashes aren't ordered, how do I get my menus to be in alphabetical order using the cgi module?
  • Comment on How do I order a hash for use as a %label list in cgi.pm?

Replies are listed 'Best First'.
Re: How do I order a hash for use as a %label list in cgi.pm?
by merlyn (Sage) on Mar 21, 2001 at 10:59 UTC
    CREATING A POPUP MENU print $query->popup_menu('menu_name', ['eenie','meenie','minie'], 'meenie'); -or- %labels = ('eenie'=>'your first choice', 'meenie'=>'your second choice', 'minie'=>'your third choice'); print $query->popup_menu('menu_name', ['eenie','meenie','minie'], 'meenie',\%labels); -or (named parameter style)- print $query->popup_menu(-name=>'menu_name', -values=>['eenie','meenie','minie'] +, -default=>'meenie', -labels=>\%labels); popup_menu() creates a menu. 1. The required first argument is the menu's name (-name). 2. The required second argument (-values) is an array reference containing the list of menu items in the menu. You can pass the method an anonymous array, as shown in the example, or a reference to a named array, such as "\@foo". 3. The optional third parameter (-default) is the name of the default menu choice. If not specified, the first item will be the default. The values of the previous choice will be maintained across queries. 4. The optional fourth parameter (-labels) is provided for people who want to use different values for the user-visible label inside the popup menu nd the value returned to your script. It's a pointer to an asso- ciative array relating menu values to user-visible labels. If you leave this parameter blank, the menu values will be displayed by default. (You can also leave a label undefined if you want to).
      Yes, thank you, I know, I read the pod too. ;)

      The problem is that when you pass -labels=>\%labels, it creates the menu in random order.

      The question is how to use this module to get an ORDERED list, not a random one. So that "Item A", "Item B", "Item C" are listed in alphabetical order versus random, which is what happens when you pass a hash of \%labels.

      -- Filmo the Klown

        The order of the hash of labels doesn't matter, because the array of values is ordered.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (2)
As of 2024-04-26 00:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found