in reply to Re: Dynamic menu options
in thread Dynamic menu options

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"?

Replies are listed 'Best First'.
Re^3: Dynamic menu options
by kennethk (Abbot) on Jul 28, 2010 at 18:46 UTC
    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;