Click to See Complete Forum and Search --> : Assigning values on the fly?


Mike Burdick
09-13-2004, 07:36 PM
Can this be done? I have a list of variables that are assigned $s1=some value, $s2=some other value, $s2 = something else etc…………………………………and so on. I won't know the exact length of it since the user can vary the input.

These numbers are to be manipulated and the following is an example of what I’m thinking:

for( $x=1;$x<$length;$x++){
$value=’s’ . $x;
$total+=$value;
}

Now this doesn’t work…but you get the idea of what I need.

Does anyone have suggestions as how to accomplish this?

Thanks!

- Mike

Nedals
09-13-2004, 08:51 PM
If your variable names are to be s1,s2,.. etc, then you are better off using an array.

my @s = (); ## declare an array
for loop
s[1]=xx, s[2]=xx, etc.

Alternatively, you could use a hash.
my %name;
for loop
name{s1}=xx, name{s2}=xx, etc

You can create variable names on the fly, but it's not recomended and will not work under 'strict', which you should be using.

Hope that helps!

Mike Burdick
09-13-2004, 10:56 PM
Nadals,

Thanks for the help. I kind of figured that what I hoped to do wouldn't work. I'll go with the array.

- Mike