First, I must suggest using an array. That would be the most simple. Then all you would need would be:
my $cnt = 0; my @q; for ( 1 .. 40 ) { $cnt++; $q[ $cnt ] = param( "q$cnt" ); }
If you are DYING to use variables named $q11, etc., it is possible. However, I recently maintained a webpage that went with this method, and I was pretty close to strangling the original writing and yelling at him to learn how to use arrays (but I refrained). Anyway, enough with my story, to do that, you would need to do something like this (I think, this is untested, and I don't know what the scope will be).
my $cnt = 0; for ( 1 .. 40 ) { $cnt++; eval( "my \$q$cnt = param( \"q\$cnt\" );" ); }
I do think you would be better off with an array in the end. I can not think of any good reason that you would necessarily need to have 40 seperately named variables instead of one array.
-Bryan
Update:
Removed 'my' from first example, thanks to merlyn pointing out my error.
In reply to Re: creating variables in a loop
by mrborisguy
in thread creating variables in a loop
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |