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

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Beginner:
Printing several variables with a loop



Apr 3, 2000, 3:07 AM

Post #1 of 3 (2176 views)
Printing several variables with a loop Can't Post

I have this program that require "" and then print the values inside. Something like this:<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


and so on...
What I'm trying to do is: for each number, print $text(number) and $image(number). The final result would be:





Thanks in advance

Administrator / Moderator

Apr 4, 2000, 7:44 AM

Post #2 of 3 (2176 views)
Re: Printing several variables with a loop [In reply to] Can't Post

Assuming you are consistent with your numbering, that is, you always will use sequential numbering (1..10), you can combine the for loop with the eval function. For example, if you have 10 image/text combinations:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

for ($i = 1; $i <= 10; $i++){
$text = eval "\$text$i";
$image = eval "\$image$i";
print "$text<BR>$image<P>";

Here's what's happening above:

1 - The for loop is incrementing the value of $i by 1 each time a loop a completed, starting at 1 and ending at 10 (your number of text/image combinations). So the first time the loop is executed, $i = 1, the second time, $i = 2, etc.

2 - The eval statement is doing 2 things: 1) concatenating the first part of the variable name \$text and the value of $i(let's say that we're at the 3rd iteration of the loop, making $i = 3) to make $text$i; 2) "evaluating" the value of \$text$i, which converts \$text$i to $text3.

The backslash in front of the $text in the eval statement is necessary... otherwise, the eval statement would look for a scalar named $text. By placing the \ in front of $text, we are telling the eval statement that we want the concatenated result of literal $text$i ($text3), not the concatenated result of a variable named $text and $i.

Very Important: never, never, never use the eval statement with unchecked user form input! This could prove to be a gaping security hole. Always use variables you define implicitly, and if you really need to eval against form input, strip all / lines, and perform error checks on the data till you pass out. You wouldn't want someone to be able to eval and print /etc/passwd, would you?

If you don't get WebTechniques magazine in the mail (get it!), bookmark and wait till May for the "Preserving Integrity" article by Matt Curtain. This article about security is a must read.

Good luck!

[This message has been edited by Jasmine (edited 04-04-2000).]


Apr 4, 2000, 9:13 AM

Post #3 of 3 (2175 views)
Re: Printing several variables with a loop [In reply to] Can't Post

Thanks for the help and the advise Jasmine. Keep the good work and this excellent site.


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

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