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

Can a monk tell me why this popup_menu wont be filled with the name of the files that i have taked from the above dir so that a user can select the desired filename?
And also how can i actually get rid of the path before the filename being showed insdie the popup_menu? Thanks
@files = <../data/texts/*.txt>; print start_form(-action=>"index.pl"); $_ =~ s/.*[\/\\](.*)/$1/; print p( {-align=>'center'}, popup_menu( -name=>'select', -value=>@ +files )); print end_form();

Replies are listed 'Best First'.
Re: Popo-up menu filled with array problem
by saskaqueer (Friar) on May 18, 2004 at 19:35 UTC

    You need to pass an array reference to the -values parameter, not the whole array itself. Adding labels to change what the user sees, you get something like this:

    use CGI qw(:all); my @files = <../data/texts/*.txt>; my %labels = ( map { $_ => +( m!([^/]+)\z! ) } @files ); print popup_menu( -name => 'select', -values => \@files, -labels => \%labels ); __END__ <select name="select"> <option value="../data/texts/foo.txt">foo.txt</option> <option value="../data/texts/bar.txt">bar.txt</option> </select>
Re: Popo-up menu filled with array problem
by sacked (Hermit) on May 18, 2004 at 19:34 UTC
    From the CGI pod:
     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".
    
    You need to pass a reference to a list to the -values key of a popup_menu:
    popup_menu( -name => 'select', -values => \@files )
    Please read the CGI documentation carefully. It is well-written and has many examples.

    --sacked