in reply to Drop-down list Option Value

Is this helpful?
#!/perl/bin/perl use strict; use warnings; use Data::Dumper; # Fish configurator my %fish = ( "Fresh water fish" => "fish1.jpg", "Salt water fish" => "fish2.jpg", "One fish" => "seuss1.jpg", "Two fish" => "seuss2.jpg", "Thanks for all the fish" => "adams.jpg", ); print qq(<form> <select name="fish"> ); # Print each fish's <option> line for my $pet (sort keys %fish) { # Note that $pet is the hash key # ... and $fish{$pet} refers to the value print sprintf qq(<option value="%s">%s</option>\n), $fish{$pet}, $pet; } print qq(</select> </form>); __OUTPUT__ <form> <select name="fish"> <option value="fish1.jpg">Fresh water fish</option> <option value="seuss1.jpg">One fish</option> <option value="fish2.jpg">Salt water fish</option> <option value="adams.jpg">Thanks for all the fish</option> <option value="seuss2.jpg">Two fish</option> </select> </form>
Where do you want *them* to go today?

Replies are listed 'Best First'.
(jeffa) 2Re: Drop-down list Option Value
by jeffa (Bishop) on Feb 02, 2003 at 09:01 UTC
    Sure it's helpful, but how about simply running your 'fish configurator' through CGI::popup_menu?
    use strict; use warnings; use CGI::Pretty qw(:standard); my %fish = ( "Fresh water fish" => "fish1.jpg", "Salt water fish" => "fish2.jpg", "One fish" => "seuss1.jpg", "Two fish" => "seuss2.jpg", "Thanks for all the fish" => "adams.jpg", ); print start_form, popup_menu( -name => 'fish', -values => [map {$fish{$_}} sort keys %fish], -labels => \%fish, ), end_form, ;
    UPDATE:
    Thanks for watching my back, Coruscate, i was caught up by the -values key -- kept thinking that it needed the hash values, not the hash keys ... oops. Thanks again. :)

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    

      jeffa++ for using CGI and presenting the right way to go about it. Just to point out however, jeffa's actual code will not work as you might hope. It took me a second to see why he was using map{} to pass the values. Then I realized that the hash was reversed in the way CGI likes to receive it. So the map works correctly. But unfortunately, CGI is not smart enough to take \%fish for the labels and flip the hash around to get the values right.

      There are two ways (that I know of) to go about fixing this. I will post both solutions. (As well, I'm hoping jeffa would plan on calling header() before start_html() :) ) First method of getting it done: attach a map{} to the passing of -labels as well. So you get the following:

      #!/usr/bin/perl -w use strict; use warnings; use CGI::Pretty qw(:standard); my %fish = ( "Fresh water fish" => "fish1.jpg", "Salt water fish" => "fish2.jpg", "One fish" => "seuss1.jpg", "Two fish" => "seuss2.jpg", "Thanks for all the fish" => "adams.jpg", ); print header(), start_form(), popup_menu( -name => 'fish', -values => [map { $fish{$_} } sort keys %fish], -labels => {map { $fish{$_}, $_ } sort keys %fish} ), end_form();

      The second way to get it done will make things much easier and will make it a little more readable. Simply flip the keys and values in the hash around and then the calls to popup_menu() will be quite simple:

      #!/usr/bin/perl -w use strict; use warnings; use CGI::Pretty qw(:standard); my %fish = ( 'fish1.jpg' => 'Fresh water fish', 'fish2.jpg' => 'Salt water fish', 'seuss1.jpg' => 'One fish', 'seuss2.jpg' => 'Two fish', 'adams.jpg' => 'Thanks for all the fish' ); print header(), start_form(), popup_menu( -name => 'fish', -values => [sort keys %fish], -labels => \%fish ), end_form();

      And from the above two snippets, there should really only be one difference: the first one will sort based on the user-viewed values (ie: 'Thanks for all the fish'), whereas the second snippet sorts based on the actual field values (ie: 'adams.jpg').


            C:\>shutdown -s
            >> Could not shut down computer:
            >> Microsoft is logged in remotely.