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

Is what I'd like to see is the variable ($name_menu) increment somehow like $name_menu1,$name_menu2, $name_menu3 etc. Not sure how to go about this..
for ($x=0; $x <= $elements; $x++) { $name_menu = "<select size='1' name='M$x'> $fname <OPTION>GUEST</OPTION> <option selected>Select a member</option> </select>" }
Cal Thanks

Replies are listed 'Best First'.
Re: need to increment a $scalar while inside for loop
by rob_au (Abbot) on Jan 02, 2002 at 07:18 UTC
    Your best way to achieve what you desire would be to make use of a more complex data structure such as an array or a hash. The other option of modifying the scalar variable name itself (symbolic references), while it can certainly be done, is not the best way to implement such a solution - To find out why symbolic references are bad, have a read through the links referenced in this node.

    For example using an array:

    my @name_menu; for (0..$elements) { splice( @name_menu, $_, 0, qq{ <select size="1" name="M$_"> $fname <option>GUEST</option> <option selected>Select a member</option> </select> } ) }

    Each element within the array could subsequently be called as $name_menu[0], $name_menu[1], etc. Alternatively, if you were to use a hash data structure, which would result in faster look ups, you might use code similar to the following:

    my %name_menu; for (0..$elements) { $name_menu{$_} = qq{ <select size="1" name="M$_"> $fname <option>GUEST</option> <option selected>Select a member</option> </select> }; }

    Each element within the hash could subsequently be called as $name_menu{0}, $name_menu{1}, etc.

    Good luck.

     

    perl -e 's&&rob@cowsnet.com.au&&&split/[@.]/&&s&.com.&_&&&print'

      Thanks, I am real interested in using the slice function. But where does it called from? Cal
Re: need to increment a $scalar while inside for loop
by cLive ;-) (Prior) on Jan 02, 2002 at 08:34 UTC
    Adding to the above, please use CGI.pm to make your life a lot easier! And avoid the references - strict will not be happy and it's easy for typos to creep in.

    Although you don't explain, I assume you want to be able to read the variables for processing afterwards, so using CGI.pm, here's an example snippet:

    my $q = new CGI; # let's assume the values creating the option html of $fname # are stored in the array @fnames, and that the values 'GUEST' # and 'Select a member' have been 'push'd onto the end. for (0..$elements) { $name_menu{$_} = $q->scrolling_list(-name => "M$_", -values=>[@fnames], -default=>['Select a member'], -size=>1 ); }
    Later, when it comes to accessing the variables sent from the form, transfer them to a hash:
    my $i=0; my %M; while (my $val = $q->param("M$i") ) { $M{$i} = $val; $i++; }

    Then all Mi elements are accessable as $M{i}

    Hope this is useful.

    cLive ;-)

Re: need to increment a $scalar while inside for loop
by jonjacobmoon (Pilgrim) on Jan 02, 2002 at 07:21 UTC
    If I understand you correctly, what you want are symbolic references which are sometimes referred (no pun intended) as soft references. From pg 254 of the Camel book:
    $name = "bam"; $$name = 1; # sets $bam ${$name} = 2; # sets $bam ${$name x 2} = 3 # sets $bambam
    I think you get the idea. I will leave the rest up to you. Hope that helps.
      FYI if someone is describing what they want to do, and it looks like they want symbolic references, I would strongly recommend warning them away from them into a different course of action.

      The standard article I refer them to for this is Avoid symbolic variables, and I tell them to read all three parts. (The third in particular has a very nice statement of why safe programming practices matter.) And then I suggest strict.pm to them.,,