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

2 dimensial array and HTML::table

Status
Not open for further replies.

eatr

Technical User
Jan 20, 2006
48
Unlike most of the other Perl modules, I've been unable to find documentation (and examples) using HTML::table.

Is it possible to create a table using this module and populate it with data from a 2-d array?

If so, could someone provide an example.


thanks
 
the first place to start is CPAN:


I have never used the module (never even heard of it until now) but it looks like it can do what you want. It also looks like a complicated way to do what is essentially an easy task: populate and html table with some data.

- Kevin, perl coder unexceptional!
 
I could see where it would be easier to create and populate html tables rather than using the module.

Question:

This is relatively simple if I have a static 2D array

what about cases where the 2d array (and thus table) size would vary (in terms of row and columns)?

How would I 'control' the <tr> and <tr> tags with a loop?

thanks

 
This might help:
Code:
my @AoA = ([1,2,3,4,5], [11,12,13,14,15], [21,22,23,24,25]);

print "<table border=1>\n";

foreach my $row (@AoA) {
    print "\t<tr>\n";

    foreach my $col (@{$row}) {
        print "\t\t<td>$col</td>\n";
    }

    print "\t</tr>\n";
}

print "</table>\n";
 
This is relatively simple if I have a static 2D array
All arrays know how big they are. Don't ever hard code 'magic' numbers in your loop control statements. As in rharsh's example, foreach normally does the job, and if you *really* need a counter, use the array itself in a scalar context as the limit
Code:
for (my $i = 0; $i < @array; $i++) {...}
'magic' numbers open you up to a world of maintenance pain, not to mention 'off by one' errors...

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]
 
Thanks for the code.

This code prints out a one dimensional array

1 2 3 4 5 11 12 13 14 15 21 22 23 24 25

How would I break it up by rows?

1 2 3 4 5
11 12 13 14 15
etc.

thanks
 
the code rharsh posted generates this html code:

Code:
<table border=1>
	<tr>
		<td>1</td>
		<td>2</td>
		<td>3</td>
		<td>4</td>
		<td>5</td>
	</tr>
	<tr>
		<td>11</td>
		<td>12</td>
		<td>13</td>
		<td>14</td>
		<td>15</td>
	</tr>
	<tr>
		<td>21</td>
		<td>22</td>
		<td>23</td>
		<td>24</td>
		<td>25</td>
	</tr>
</table>

which does what you are asking.

- Kevin, perl coder unexceptional!
 
You're right.

Sorry. When I was running this earlier I wasn't getting a 2-d table for some reason.

Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top