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

I have a semi-quick question, I'm sure its very easy, as I could have done it in C++, but because of my lack of experience - - I don't know how to do it in perl.
while ($ucount <= $icount) { $radio_$ucount = param('radio_$ucount'); $textarea$ucount = param('textarea$ucount'); $ucount++; } $ucount = 1;

Do I need to do some sort of "PRINT" to be able to use the variable $ucount as a 'modifier' so it should use this code:
$radio_1 = param('radio_1'); $textarea1 = param('textarea1'); $ucount++; (UCOUNT IS NOW 2) $radio_2 = param('radio_2'); $textarea2 = param('textarea2'); $ucount++; (UCOUNT IS NOW 3) etc etc...
I'm sure I'm most likely missing something simple, but its those simple things that drive me crazy!

Replies are listed 'Best First'.
Re: Using Variables with Variables
by Joost (Canon) on Apr 02, 2007 at 14:18 UTC
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Using Variables with Variables
by polettix (Vicar) on Apr 02, 2007 at 15:14 UTC
    Please take care to read Joost's answer carefully, spotting the needed differences. Perl has two quoting approaches: single quotes (that basically don't interpolate) and double quotes (that do interpolate). So:
    my $variable = 42; print 'this is $variable'; # prints this is $variable print "this is $variable"; # prints this is 42
    More on this in perlop, under "Quote and Quote-like Operators".

    Flavio
    perl -ple'$_=reverse' <<<ti.xittelop@oivalf

    Don't fool yourself.
Re: Using Variables with Variables
by blue_cowdawg (Monsignor) on Apr 02, 2007 at 14:19 UTC
        Do I need to do some sort of "PRINT" to be able to use the variable $ucount as a 'modifier' so it should use this code:

    You mean you want to do something like:

    for my $ix (1..10){ $textarea[$ix-1] = param('textarea' . $ix); }
    I'm not sure I'd use a separate variable where an array would make more sense. Hence my use of the array @textarea in my example.


    Peter L. Berghold -- Unix Professional
    Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg
Re: Using Variables with Variables
by saintly (Scribe) on Apr 02, 2007 at 15:25 UTC
    This particular instance calls for arrays, like the others have said. However, for reference, you can do what you were trying to do like this:
    ${ 'radio_' . $ucount } = param( "radio_$ucount" ); ${ "textarea$ucount" } = param( 'textarea' . $ucount );

    Note that
    • Variables won't be interpolated into a string that uses single-quotes.
      print "something$ucount"; # prints 'something1' print 'something$ucount'; # prints 'something$ucount'
    • You can use the concatenation operator '.' (spot) to create single strings, which is often useful for clarity.
      $x = "something$ucountElse"; # probably tries to interpolate a variable called # $ucountElse, ambiguous $x = "something" . $ucount . "Else"; # Unambiguous
    • You can rewrite $name to ${'name'}, and you can interpolate this syntax too:
      $x = "something${ucount}Else";

Re: Using Variables with Variables
by Ovid (Cardinal) on Apr 03, 2007 at 15:38 UTC

    As Joost mentioned, an array would fix your immediate problem. However, it's much simpler to fix. Most folks don't seem to know this, but the following is a legal query string (semicolons are the preferred delimiter, but people don't seem to know that, either):

    sport=football;sport=basketball;sport=sheep%20tossing

    Yes, that has three 'sport' parameters. Then you can slurp all of them into an array at once:

    my @sports = param('sport');

    The param function in CGI.pm returns the first value in scalar context and all values in list context. Then, rather than having to have differently named fields in your form and having to remember to keep your code in synch with the field names, you give all identical types in the form the same name and have your code iterate over them without worrying about the indexes.

    Cheers,
    Ovid

    New address of my CGI Course.

Re: Using Variables with Variables
by Trihedralguy (Pilgrim) on Apr 02, 2007 at 16:27 UTC
    Sorry about all this non-sense...I'm just having a bad day I guess..Joost's first method works like a charm I'm just having a problem with my $icount variable. I was using it at the top of my script and for some reason it isn't keeping its value throughout the script, thus causing all this stupid confusion.