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

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
naming variables.

 



alex_tea
newbie

Oct 21, 2001, 9:05 PM

Post #1 of 2 (849 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!

thanks.



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


rGeoffrey
User / Moderator

Oct 27, 2001, 1:12 PM

Post #2 of 2 (833 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...


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

rather than...


Code
${$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