CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Beginner:
naming variables.



Oct 21, 2001, 9:05 PM

Post #1 of 2 (995 views)
naming variables. Can't Post

i know this will be stupid and easy. i've looked through the faq, but couldn't see anything...

anyway, i want to be able to assign data to dynamically named variables in a for loop, right now i am using this code:

$varName = $formdata{'varName'};
$arrayLength = $formdata{'arrayLength'};

for ($i = 0; $i<=$arrayLength; $i++) {

$arrayName = $varName . $i;
$arrayName = $formdata{'$varName . $i'};


but all that would do is reassign the value of $arrayName. i know that in actionscript i would use this:

for ($i = 0; $i<=$arrayLength; $i++) {

$arrayName = $varName . $i;
eval($arrayName) = eval($formdata{'varName'} . $i;


but i'm not sure if that would work.

any help would be much appreciated!


(This post was edited by alex_tea on Oct 21, 2001, 8:15 PM)

User / Moderator

Oct 27, 2001, 1:12 PM

Post #2 of 2 (979 views)
Re: naming variables. [In reply to] Can't Post

Creating variable names in the middle of a perl script is problematic at best, and really nasty with 'use strict'. If you really need this feature consider using a hash where the keys are what you wanted to call your legitamate variables...

$variables{"$varName$i"} = 1;

rather than...

${$varName$i} = 1;

I found two old threads that might have something useful to say on the subject...

Is it possible, with a reference, to determine the name of a variable.
loading dynamic configuration variables


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives