Results 1 to 4 of 4
Hello everyone,
I hope someone can help me out with the following.
I have a 2D matrix with size 20x8 (20 rows & 8 columns) and I'm trying to print ...
- 06-10-2009 #1Linux User
- Join Date
- Jul 2007
- Location
- Greece
- Posts
- 277
printing the elements of a 2D array into an HTML table
Hello everyone,
I hope someone can help me out with the following.
I have a 2D matrix with size 20x8 (20 rows & 8 columns) and I'm trying to print each element to an HTML file. The easiest way to do that is to print each line separately:
It works fine this way. However I know that with a for loop you can simplify things and instead of writing 20 print statements you can do it in a few lines. Can anyone tell me how I can achieve this, please? How should I go about it? I find it a bit tricky to apply a for loop in this case. I tried using a foreach loop like that:Code:1st Row print "<TR> <TH>pac1</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>pac2</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>";
but the result I get is a 20x2 table instead of 20x8 and the elements comes up as hexadecimalCode:print "content-type: text/html \n\n"; #The header # SET A COUNT VARIABLE $count = pac1; # BEGIN THE LOOP foreach $data(@data) { #print "<tr><th>$count</th><th>$data</th></tr>"; $count++; }
No idea why this is happening.
Any suggestions please?Code:pac1 ARRAY(0x628ea0) pac2 ARRAY(0x628f60) pac3 ARRAY(0x629030) pac4 ARRAY(0x629100) pac5 ARRAY(0x6291d0) pac6 ARRAY(0x6292a0) pac7 ARRAY(0x629370) pac8 ARRAY(0x629440) pac9 ARRAY(0x629510) pac0 ARRAY(0x6295e0) pac1 ARRAY(0x6296b0) pac2 ARRAY(0x629780) pac3 ARRAY(0x6298f0) pac4 ARRAY(0x629a40) pac5 ARRAY(0x629850) pac6 ARRAY(0x631470) pac7 ARRAY(0x631540) pac8 ARRAY(0x631610) pac9 ARRAY(0x6316e0) pac0 ARRAY(0x631850)
Thanks a lot guys
- 06-11-2009 #2Linux User
- Join Date
- May 2008
- Location
- NYC, moved from KS & MO
- Posts
- 251
1. Create a python script, let's call it t2h.py
2. use t2h.py to process your data file, any of the following should return the same results:Code:#!/bin/env python import fileinput def gen_html(): HTML="" row_count=1 for line in fileinput.input(inplace=False): TH=['pac'+str(row_count)] for word in line.split(): TH.append( '<TH>'+word+'</TH>' ) ROW="".join(th for th in TH) HTML+="<TR>"+ROW+"</TR>\n" row_count+=1 return HTML if __name__=="__main__": print gen_html()
cat data|python t2h.py
python t2h.py < data
python t2h.py data
- 06-15-2009 #3
So I think you're using Perl?
If you have a list of lists in Perl, what you actually have is a list of arrayrefs. An arrayref is basically a scalar that points to an actual array. You're on the right track, but you missed one element.
You have a list of lists. The first list contains a bunch of rows. Each row (lists inside the main list) contains a bunch of columns. Recognizing this, our code becomes simple:
You see? We loop through every element of the inner loops as well, and thus get all of our data.Code:#!/usr/bin/perl print "content-type: text/html \n\n"; #The header # SET A COUNT VARIABLE $count = 1; # BEGIN THE LOOP foreach $row (@data) { print "<tr>"; print "<td>$count</td>"; foreach $col (@$row) { print "<td>$col</td>"; } print "</tr>"; $count++; }
Does this help?DISTRO=Arch
Registered Linux User #388732
- 07-22-2009 #4Linux User
- Join Date
- Jul 2007
- Location
- Greece
- Posts
- 277
I was browsing my threads and realized that I didn't even say thank you for replying.

I know it's a bit late now but thank you guys for the help.


Reply With Quote