Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Jun 2009
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    printing 2D matrix elements into an HTML table

    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:
    Code:
    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

  • #2
    Senior Coder
    Join Date
    Mar 2006
    Posts
    1,274
    Thanks
    2
    Thanked 39 Times in 38 Posts
    When you have a list like an array, you can use a loop to process all or some of its elements.

    Code:
    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.

  • #3
    New Coder
    Join Date
    Jun 2009
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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:

    Code:
    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]

  • #4
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    try this way:
    Code:
    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

  • #5
    New Coder
    Join Date
    Jun 2009
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Worked a treat, nice one oesxyl

    Thank you both very much for helping me.
    THANK YOU


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •