
japhy
Enthusiast
Mar 30, 2000, 8:12 AM
Post #2 of 6
(439 views)
|
|
Re: Getting Parameters with a Loop, Use several $var(x) in a loop
[In reply to]
|
Can't Post
|
|
You're asking about symbolic references: <BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR> for $i (1 .. 10) { # a loop, Perl-style ${"var$i"} = param("field$i"); } </pre><HR></BLOCKQUOTE> Now that you've seen it, don't use it! Symbolic references are "left over" from Perl 4 before there was any means of real references (hard references). But the point here is that if you have a list of variables, $foo1, $foo2, $foo3, etc., you should use an array. <BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR> @var = map param("field$_"), 1 .. 10; # then you can do print $var[3]; # since arrays start at 0, $var[3] is param("field4") </pre><HR></BLOCKQUOTE> That map() function is rather useful, and saves you from doing a loop with a for() statement instead: <BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR> for (1 .. 10) { push @var, param("field$_"); } </pre><HR></BLOCKQUOTE>
|