Beefy Boxes and Bandwidth Generously Provided by pair Networks DiBona
Perl-Sensitive Sunglasses
 
PerlMonks  

dynamically initializing variables with perl

by db2admin (Acolyte)
on Jan 07, 2003 at 06:58 UTC ( #224880=perlquestion: print w/ replies, xml ) Need Help??
db2admin has asked for the wisdom of the Perl Monks concerning the following question:

Is it possible to initialize variables using perl? I have tried the following but receive a syntax error:
for ($i=1; $i<= $msgcount; $i++) { $C$checkcnt = param($C$checkcnt); $checkcnt++; }
What I have is an email system that has a 'delete' checkbox for each message in the user's mailbox. So I am trying to create the html with the check boxes based upon the number of messages that the user has.

Does anyone have any ideas?

Comment on dynamically initializing variables with perl
Download Code
Re: dynamically initializing variables with perl
by PodMaster (Abbot) on Jan 07, 2003 at 07:09 UTC
    >Is it possible to initialize variables using perl?

    Yes.

    >I have tried the following but receive a syntax error

    What do you think $C$checkcnt should evaluate to?

    That's your syntaxt error.

    You should read Why it's stupid to `use a variable as a variable name' Part 1 2 3.

    You should also read `perldoc perldata', and `perldoc perlsyn', as it is clear you need to.


    MJD says you can't just make shit up and expect the computer to know what you mean, retardo!
    ** The Third rule of perl club is a statement of fact: pod is sexy.

Re: dynamically initializing variables with perl
by busunsl (Vicar) on Jan 07, 2003 at 07:11 UTC
    Looks like you want to have variables called $C1, $C2, a.s.o.

    Normally you use an array for things like that:

    my @C; for (my $i=0; $i< $msgcount; $i++) { $C[$i] = param("C$i"); }
    or without the for loop:
    my @C = map { param("C$_") } 0..$msgcount - 1;
    Update: Changed $C[0] to $C[$i]
    Thanks to Hofmator for this catch.

      I wouldn't use the map, though it's popped up a few times recently. What if someone passed parameters such as this?

      C1 = 1 C2 = 1 C2 = 0 C2 = 1 C3 = 0

      You'll end up with an array of (1, 1, 0, 1, 0) when you're only expecting three checkboxes.

        I suppose the checkboxes are created dynamically since it is a mail app.
        So they will be named uniquely and this shouldn't be a problem.
      Thank you for your help. I tried the for loop but it doesn't initialize the variables $C1, $C2, $C3 even though the $msgcount is a value of 3. Just from looking at it, @C is never populated with any values so I would suppose @C would have to be set to the variables $C1, $C2 ... which I don't think will even be syntatically correct (i.e. @C = ($C1, $C2, ...)).

      The map function does work when the checkbox is set to ON (when checked) but if the checkbox is not set, there is no value returned and the map function will not put anything into the array element for that checkbox. So if I were to delete message 1 and message 3, there is no way to correspond the array elements with a particular message since the array looks like this:

      $C[0] = "ON"; $C[1] = "ON";
      When in reality, it should look like:
      $C[0] = "ON"; $C[1] = ""; $C[2] = "ON";
      I am not aware of an option with the checkbox to have two settings. It's either ON or not ON (i.e. no value).

      Thanks again.

      David K.

        Try the following map (untested):
        my @C = map { param("C$_") || 'OFF' } 0..$msgcount - 1;
        It should give something like:
        $C[0] = "ON"; $C[1] = "OFF"; $C[2] = "ON";

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://224880]
Approved by virtualsue
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (6)
As of 2014-04-23 18:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (553 votes), past polls