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:
Getting Parameters with a Loop, Use several $var(x) in a loop

 



Rivotti
User

Mar 30, 2000, 5:33 AM

Post #1 of 6 (771 views)
Getting Parameters with a Loop, Use several $var(x) in a loop Can't Post

Hi:
I created a form with 10 fields. To get the parameters a need to use the lines:
<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

$var1= param('field1');
$var2= param('field2');
$var3= param('field3');
$var4= param('field4');
$var5= param('field5');
...
$var10= param('field10');
</pre><HR></BLOCKQUOTE>
Is it possible to do this operation with a loop? like:
<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

for ($j=1; $j<=10; $j++)
{ $var(number)= param('field(number)'); }</pre><HR></BLOCKQUOTE>

The same way, can I use all the parameters using a loop? like :
<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

for ($j=1; $j<=10; $j++)
{ print "$var(number)\n"; }</pre><HR></BLOCKQUOTE>

Please make it possible.
Rivotti


japhy
Enthusiast

Mar 30, 2000, 8:12 AM

Post #2 of 6 (772 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>


Rivotti
User

Mar 30, 2000, 2:13 PM

Post #3 of 6 (771 views)
Re: Getting Parameters with a Loop, Use several $var(x) in a loop [In reply to] Can't Post

Hi japhy:

I've tried what you told me to do but I got the following error:

Undefined subroutine &main::param called at ...

Another thing:
I have the variable $xtime=4 and i want to get @var param $xtime. Then test each one of $var - unless(!$var[number]) - and then print each one. All of this in a loop. Is it possible?

Thanks
Rivotti


Cure
User

Mar 30, 2000, 2:42 PM

Post #4 of 6 (771 views)
Re: Getting Parameters with a Loop, Use several $var(x) in a loop [In reply to] Can't Post

Hi

you need this at the top of your cgi-script
use CGI qw(:standard);
to pevent the Undefined subroutine &main::param called at ...

use CGI qw(:standard) -> this will import ":standard" set of function definitions into your cgi-script.

Cure


Rivotti
User

Mar 31, 2000, 12:29 AM

Post #5 of 6 (771 views)
Re: Getting Parameters with a Loop, Use several $var(x) in a loop [In reply to] Can't Post

Thanks, you found the "Cure" for my problem


Rivotti
User

Mar 31, 2000, 1:45 AM

Post #6 of 6 (771 views)
Re: Getting Parameters with a Loop, Use several $var(x) in a loop [In reply to] Can't Post

What if I'm getting variables form a file using require "file.pl"?
<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

$text1="text1";
$text2="text2";
$text3="text3";
$text4="text4";
...
$text10="text10";</pre><HR></BLOCKQUOTE>
Is there a way to teste if the var is empty and, if not, print the var text?
Something similar to this:
<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

for ($j=1; $j<=10; $j++)
{ unless(!$text[number])
{ print $text[number] }
}</pre><HR></BLOCKQUOTE>

I'm sure this code is not correct. Is there a correct way of doing this. This is a simple example: The program I'm working have 20 variables. As you can see it's important to know this to avoyd writing useless lines to my program

Thank you very much
Rivotti

 
 


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

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