in reply to (cLive ;-)Re: nesting foreach statements
in thread nesting foreach statements

Thank you, but my problem is a bit more complex. I should try to explain better. 1) I have a form which contains a multichoice box in that format:
<select name="colors" multiple size=5 "> <option>Blue <option>Red <option>Yellow <option>Green </select>
2) When user fill out form, they can choose multiple options. After they submit, it saves the options into a flat text file, along with the other form's answer: userid|password|name|email|Blue,Red|comments 3) The user has the option to come back in later time and edit his info. It is prompted with an edit form on which his previous answers are already inserted. In this case I would need the edit form to present the "colors" field in this way:
<select name="colors" multiple size=5 "> <option selected>Blue <option selected>Red <option>Yellow <option>Green </select>
I am limited with perl and I have tried to basically read the array from the choice of colors: @colors = ("Blue","Red","Yellow","Green"); make the chosen colors scalar into an array and read it: @chosen = ("Blue","Red"); an then tried to compare both but all I can come up with is a bunch of nested foreach statements which is not good:
foreach colors { foreach chosen { if $chosen eq $color { $editchosen = <option selected>$color } else { $editchosen = <option>$color } } }
Anyway, you can tell that I am not starting well with my thinking. Help! Thank you.

Replies are listed 'Best First'.
(cLive ;-)Re: nesting foreach statements
by cLive ;-) (Prior) on Apr 07, 2002 at 04:38 UTC
    Like this?
    #!/usr/bin/perl -w use strict; use CGI; my $q = new CGI; my @colors = qw(red yellow pink green orange purple blue); my @chosen = qw(red green purple); print $q->header, $q->start_html, $q->start_form, $q->scrolling_list(-name => 'color', -values => \@colors, -default => \@chosen, -size => 5, -multiple => 'true' ), $q->submit, $q->end_form, $q->end_html; exit(0);
    cLive ;-)