in reply to Dynamic menu options

I suspect the issue is that you are not storing your strings in $dmenu as you go through. You seem to be using a foreach loop in place of map - Perl tells you this is a syntax error. Perhaps you mean:

#!/usr/bin/perl use CGI::Carp qw(fatalsToBrowser set_message); use CGI qw(-oldstyle_urls :standard); print header(); my @acc = qw(12345 67895 123445 112288 3736666 445899); my $c=0; my $dmenu="<select name=dropdown>"; foreach my $accs(@acc) { $c++; $dmenu .= "<OPTION value=$c>$accs</OPTION>"; } $dmenu .= "</select>"; print $dmenu;

Note I have used the concatenating assignment operator .= (see Assignment Operators, Additive Operators).

Update: Copy paste fail fixed.

Replies are listed 'Best First'.
Re^2: Dynamic menu options
by Anonymous Monk on Jul 28, 2010 at 18:11 UTC
    Yes it makes sense, but it is needed to remove the "my" from $dmenu inside "foreach" and last line of the code.
    How could I delimit the values on the array to display only 4 numbers instead of all of them? Cause sometimes I will have years stored in there and I don't need to display more them 4 years starting from current year backwards, could I use "pull"?
      If "pull" corresponds to shift, then yes. You can swap your loop variable to $c (though the only single-letter variables that are generally considered good form are $i, $j, $x, $y and $z) and use a shift to get the array member, which would destroy the array in the process. You could also use indices on the array to the same end.

      #!/usr/bin/perl use CGI::Carp qw(fatalsToBrowser set_message); use CGI qw(-oldstyle_urls :standard); print header(); my @acc = qw(12345 67895 123445 112288 3736666 445899); my $dmenu="<select name=dropdown>"; my $count = 4; foreach my $c (1 .. $count) { my $accs = shift @acc; $dmenu .= "<OPTION value=$c>$accs</OPTION>"; } $dmenu .= "</select>"; print $dmenu;