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

I want to use an array of values to fill in a pull down menu selection of a CGI menu; however, when I try to use an array, it only lists the first value. Looking at the CGI input, the menu usually takes something like
print "<tr><td><B>Category:</B></td><td>"; print $query->popup_menu( -name=>'pubcategory', -values=>['a', 'b', 'c'], -default=>'None'); print "<td></tr>\n";
I'm trying to put in:
print "<tr><td><B>Category:</B></td><td>"; print $query->popup_menu( -name=>'pubcategory', -values=>@publication_categories, -default=>'None'); print "<td></tr>\n";
(@publication_categories is simply generated from http://devo.wi.mit.edu/~dzhao/cgi-bin/pub_cat.txt) However, no matter what I do, it only lists the first value of the array. Should I hack the array into a giant string that looks like ['a','b','c'...]? That seems like an ugly solution. Can anyone help? Thanks

Replies are listed 'Best First'.
Re: using an array to propagate a CGI menu
by bjelli (Pilgrim) on May 14, 2001 at 23:55 UTC

    The problem is that popup_menu demands a reference to an array, not an array.

    There are two common ways of creating a reference to an array:

    you quote the man page of CGI, where a reference to an anonymous array is used:

    -values=>['a', 'b', 'c']

    But what you want is to use an existing (named) array, and create a reference to it:

    -values=>\@publication_categories

    There's a lot about refernces in the standard perl documentation: perlre is about references, perldsc about various intresting data structures you can build with them.

    --
    Brigitte    'I never met a chocolate I didnt like'    Jellinek
    http://www.horus.com/~bjelli/         http://perlwelt.horus.at
Re: using an array to propagate a CGI menu
by boo_radley (Parson) on May 14, 2001 at 22:59 UTC
    try
    -values=>\@publication_categories
    instead.
    this is CGI's POD. The link is to the online version located on CPAN, so it may be a different one than what you've got on your system, but perldoc cgi at a command line should present you with the appropriate documentation on your system.
Re: using an array to propagate a CGI menu
by Hero Zzyzzx (Curate) on May 14, 2001 at 23:01 UTC

    Escape the @publication_categories array with a "\", like so:

    -values=>\@publication_categories

    and your code should work fine. Check the CGI.pm docs at the end of this section here for usage info.

    Good luck!!

      lol... thanks guys... I was reading on a different CGI doc site that had it listed as "\@array", which confused me and didn't work...