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

Hi Monks!

I am trying to have a foreach loop going through a select menu and assign that to a variable to be printed somewhere else, can't get it to work and I know it is possible, any help!!!
Here is the code:
#!/perl/bin/perl print "Content-Type: text/html\015\012\015\012"; @cities = ("Seattle", "Odense", "Venice", "Lewes", "Olso"); # Here its no problem print "<select>\n"; print "<option>City Name</option>\n"; foreach $city (sort @cities){ print '<option value="'.$city.'">'.$city.'</option>'."\n"; } print "</select>\n"; # But here is my nightmare $test="<select><option>City Name</option>"; foreach $city (sort @cities){ print "<option value=".$city.">".$city."</option>"; } print "</select>\n"; print $test;

Thanks a lot!

Replies are listed 'Best First'.
Re: Select form Hell!
by Cody Pendant (Prior) on Jul 19, 2007 at 02:35 UTC
    I think what you're trying to do is this:
    $test="<select><option>City Name</option>"; foreach $city (sort @cities){ $test .= "<option value=".$city.">".$city."</option>"; } $test .= "</select>\n"; print $test;


    Nobody says perl looks like line-noise any more
    kids today don't know what line-noise IS ...
      That was it...Thank you very much, I just couldn't think anymore. Well done!!
Re: Select form Hell!
by GrandFather (Saint) on Jul 19, 2007 at 03:06 UTC

    You may like:

    use strict; use warnings; use HTML::Template; my $template = HTML::Template->new (arrayref => [<DATA>]); my @cities = ("Seattle", "Odense", "Venice", "Lewes", "Olso"); $template->param (SelectCity => [map {{City => $_}} @cities]); print $template->output(); __DATA__ <html> <head><title>Test Select</title> <body> <select> <TMPL_LOOP NAME="SelectCity"><option value="<TMPL_VAR NAME=City>"> +<TMPL_VAR NAME=City></option> </TMPL_LOOP> </select> </body> </html>

    Prints:

    <html> <head><title>Test Select</title> <body> <select> <option value="Seattle">Seattle</option> <option value="Odense">Odense</option> <option value="Venice">Venice</option> <option value="Lewes">Lewes</option> <option value="Olso">Olso</option> </select> </body> </html>

    BTW, I strongly recommend that you use strictures (use strict; use warnings;).


    DWIM is Perl's answer to Gödel
Re: Select form Hell!
by perrin (Chancellor) on Jul 19, 2007 at 03:18 UTC
    > SELECT FROM hell WHERE circle = 9; ** FOUND 8 RESULTS: 'in-house templating systems', 'chained ternary operators', 'strict turned off just until I get this working', 'gratuitous use of AUTOLOAD', 'my $foo = 1 if $bar', 'hand-rolled CGI data parsers', 'static code generators', 'optimization without profiling'
Re: Select form Hell!
by tinita (Parson) on Jul 19, 2007 at 11:48 UTC
    following the example from GrandFather, here is mine =)
    use HTML::Template::Compiled; my $htc = HTML::Template::Compiled->new( scalarref => \"<select><%html_option cities %></select>", plugin => [qw(::HTML_Tags)], ); $htc->param( cities => [ 'Hamburg', # selected map { [$_, $_] } qw(Berlin Hamburg North_Adams)], ); print $htc->output;
    Output:
    <select><option value="Berlin" >Berlin</option> <option value="Hamburg" selected="selected">Hamburg</option> <option value="North_Adams" >North_Adams</option></select>
    or even cleaner in my opinion: