option items sorted in pop-up menus
Is there any way to use CGI.pm and still have option items sorted in pop-up menus? CGI.pm provides cook ways to create HTML form. To create a pop-up menu, I can do something like this: # create a hash of terms my @terms = MyLibrary::Term->get_terms(sort => 'name'); my %terms; foreach (@terms) { $terms{$_->term_id} = $_->term_name } ... $html .= $cgi->popup_menu(-name => 'id', -values => \%terms); where: -name is the name of the parameter I want returned -values is a reference to a hash containing id and value pairs. The problem is that -values is a hash, and when CGI.pm displays the pop-up menu the items do not come out sorted. (They were inserted into the hash in a sorted order.) Is there some I can get CGI.pm to output the popup items in sorted order, or should I write my own little function to do this for me? -- Eric Lease Morgan University Libraries of Notre Dame
Re: option items sorted in pop-up menus
Eric, $html .= $cgi->popup_menu(-name => 'id', -values => \%terms); where: -name is the name of the parameter I want returned -values is a reference to a hash containing id and value pairs. The problem is that -values is a hash, and when CGI.pm displays the pop-up menu the items do not come out sorted. (They were inserted into the hash in a sorted order.) Is there some I can get CGI.pm to output the popup items in sorted order, or should I write my own little function to do this for me? According to the CGI man page I have, -values takes an _array_ reference not a hash reference. I think you need to use the -lables option which does take a reference to a hash. The -lables hash lets you display one thing to the user and return another value to your script. Regards, Ashley. -- Ashley Sanders [EMAIL PROTECTED] Copac http://copac.ac.uk -- A MIMAS service funded by JISC
RE: option items sorted in pop-up menus
Hi Eric, > -Original Message- > >$html .= $cgi->popup_menu(-name => 'id', -values => \%terms); > > where: > >-name is the name of the parameter I want returned >-values is a reference to a hash containing id and value pairs. > > The problem is that -values is a hash, and when CGI.pm displays the > pop-up menu the items do not come out sorted. (They were inserted into > the hash in a sorted order.) >From the docs [1] it appears that values should actually be an array reference so maybe the following would work: $html .= $cgi->popup_menu( -name => 'id', -values => [ sort keys %terms ], -labels => \%terms ); It just creates an anonymous array of sorted keys from the terms hash. The labels option is used to show the term names rather than the IDs in the menu. I haven't actually tested it, but give it a shot (see the docs for more info) -Brian [1] http://search.cpan.org/dist/CGI.pm/CGI.pm#CREATING_A_POPUP_MENU