Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Printing the result of a foreach loop onto a html template

Status
Not open for further replies.

chrismassey

Programmer
Aug 24, 2007
264
GB
Hello,

Once a foreach loop has completed its process I want to be able to take the result and print it onto a HTML template, rather than printing each $_ every time it loops round.

Is there a way to store all the $_ into a variable (say $html_contents) so that I can print a HTML template, and print $html_contents where needed.

Thanks
 
you can put an array between double-quotes to print it:

print "@arrry";

that adds a space between each array element by default. You can change the value of $" (I thinks thats it) to alter the behavior.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Thanks for replying so quickly,

So this is an example code (before)

Code:
@array = (one, two, three, four, five);
foreach (@array) {
print "$_";
}

Now because I don't want to simply print $_ because by default it will print plain text etc, I have laid each $_ into a table with a checkbox next to each one. Therefore I cannot just print @array unless there is a way to take each of the array elements and place them neatly into a table row before putting all those elements back into @array. I hope you understand.

One more question: if I were to do this...

Code:
$no = 1;
$test{$no}

would this create the scaler variable $test1, so that I can increment 1 in a loop and create an unlimited number of scalers?

Thanks again
 
If it helps, this gives you an idea of what i'm trying to produce...

Here is the script i'm working on...
As you can see the foreach loop prints a number of rows

And here is the template I want to print the result of the foreach loop into

(but obviously I will remove the 2 path textboxes)
 
quick and dirty example, should give you an idea"

Code:
@array = (one, two, three, four, five);
my $html = '<table>';
foreach (@array) {
$html .= "<tr><td>$_"</tr></td>;
}
$ html .= '</table>';

now you can use $html as often as you like in your script.

One more question: if I were to do this...

CODE
$no = 1;
$test{$no}

would this create the scaler variable $test1, so that I can increment 1 in a loop and create an unlimited number of scalers?

No. It would create a hash with a key that is named "1".

I don't understand the last part of the question:

so that I can increment 1 in a loop and create an unlimited number of scalers?








------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top