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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Output table

Status
Not open for further replies.

mossv2

MIS
Mar 4, 2010
5
US
Code:
...
...
if($Q::host1 != "") {
   push (@f,$Q::host1);
}
if($Q::host2 != "") {
   push (@f,$Q::host2);
}
$num = scalar @f;
print "<table>";
   print "<tr bgcolor='blue'>
   <td><font face='arial' size='2' color='white'><b>host</b></td>
   </tr>";
for ($i = 0; $i <= ($num - 1); ++ $i) {
   $vv = $f[$i];
   print "<tr bgcolor='beige'>
   <td><font face='arial' size='2'>$$vv</td>";
print "</tr>";
print "</table>";

The problem is my table isnt' being printed. I have the header like so:
Code:
host
The header is in blue, but then each of the table entries should be in beige, but I'm not getting any table output.
Code:
host    
host1  
host2
What it should output. But with the above code it isn't printing the hostname in the table.
 
Any reason your not just doing
Code:
for my $vv (@f) {
 print "$vv\n";
}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
Doing it that way still fails to print anything in the table.
 
Then your not putting anything in the array.. put some print statements by your push's and see if they print.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
No. If I print $f[1] after $num then it prints the correct value. It just doesn't seem to be doing it inside the for loop.
 
[ol][li]You don't have a closing } on the for loop in the original post.[/li][li]Why do you have $$vv in the print line instead of $vv? use strict; and use warnings; are your friends, and would have shown the use of the uninitialised variable.[/li][li]If you use @f in a scalar context (as you are doing above), you don't need the explicit scalar keyword, Perl will figure it out.[/li][li]If you had used < instead of <= on the for loop condition, you wouldn't need to subtract 1 from $n to do the comparison.[/li][li]Normally you see $i++ rather than ++$i on the for loop.[/li][li]Travis' solution is a lot cleaner, as you don't need the loop counter at all, and even the $vv is redundant as $_ will hold the same value if you code for (@f) { ... }.[/li][li]Start by getting your loop working as Travis suggests, and then add the window dressing of the surrounding HTML afterwards.[/li][li]Consider getting rid of all the embedded formatting and put it in a style sheet (*.css file) instead. It will clean up your code massively, not to mention allowing you to re-skin the whole site at a whim AND support disabled users more easily.[/li][/ol]

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top