...

View Full Version : printing 2D matrix elements into an HTML table



goude
06-11-2009, 10:44 AM
Hello everyone,

I have a 2D matrix with size 20x8 (20 rows & 8 columns) and I'm trying to print each element to an HTML table. The easiest way to do that is to print each line separately:


1st Row
print "<TR> <TH>row1</TH> <TH>$data[0][0]</TH> <TH>$data[0][1]</TH> <TH>$data[0[2]</TH> <TH>$data[0][3]</TH> <TH>$data[0][4]</TH> <TH>$data[0][5]</TH> <TH>$data[0][6]</TH> </TR>";

2nd Row
print "<TR> <TH>row2</TH> <TH>$data[1][0]</TH> <TH>$data[1][1]</TH> <TH>$data[1]2]</TH> <TH>$data[1][3]</TH> <TH>$data[1][4]</TH> <TH>$data[1][5]</TH> <TH>$data[1][6]</TH> </TR>";

It works fine like that but is there a way to simplify things? I mean there must be a better way to do that instead of writing 20 print statements.
Does anyone know a better way of doing this? Any suggestions. please?

Thanks

KevinADC
06-11-2009, 06:10 PM
When you have a list like an array, you can use a loop to process all or some of its elements.


for (@array) {
for (@{$_}) {#<-- dereference the arrays in @array
print "$_";
}
print "\n";
}

That is the basics of what you want to do, see if you can figure out how to get it to print your html output properly. If you can't get it post the code you wrote.

goude
06-12-2009, 10:41 AM
Thanks for your reply KevinADC

Since my last post I managed to go a bit further using a foreach loop but I don't get the desired output. Here is what I've done so far:


print "content-type: text/html \n\n"; #The header

# SET UP THE HTML TABLE
print "<table border='3'>";

my ($fileread, $filein, @data);
$filein = '/var/www/cgi-bin/intxtfile';

#OPEN DATA FILE AND PUT THEM INTO A 2D ARRAY
open ($fileread, '<', $filein) || die ("Can't open: $filein $!");

while (<$fileread>) {
chomp;
push @data , [split(/,/)]; #split elements on comma's
}
close ($fileread);


my @row= ("row1","row2","row3","row4","row5","row6", "row7", "row8","row9","row10", "row11",
"row12","row13","row14","row15","row16","row17", "row18", "row19","row20");

my $row_ref = \@row;

#START LOOP
foreach $row (@$row_ref) {
print "<TR> <TH>$row</TH> <TH>$data[0][0]</TH>
<TH>$data[0][1]</TH> <TH>$data[0][2]</TH> <TH>$data[0][3]</TH>
<TH>$data[0][4]</TH> <TH>$data[0][5]</TH> <TH>$data[0][6]</TH> </TR>";
}


What I'm doing above is asking Perl to print the same elements for each row but I can't figure out how to make it print different elements for each row. For example:

row1 = $data[0][0] $data[0][1] $data[0][2] $data[0][3] $data[0][4] $data[0][5] $data[0][6]
row2 = $data[1][0] $data[1][1] $data[1][2] $data[1][3] $data[1][4] $data[1][5] $data[1][6]
.
.
row20=$data[19][0] $data[19][1] $data[19][2] $data[19][3] $data[19][4] $data[19][5] $data[19][6]

oesxyl
06-12-2009, 11:15 AM
try this way:


foreach my $i (0 .. 20){
print '<tr>';
print '<td>',$row[$i],'</td>';
foreach my $j (0 .. 8){
print '<td>',$data[$i][$j],'</td>';
}
print '</tr>';
}


best regards

goude
06-12-2009, 11:44 AM
Worked a treat:thumbsup:, nice one oesxyl

Thank you both very much for helping me.
THANK YOU



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum