Search code examples
perldynamic-data

How can I dynamically generate an HTML tables containing 100 rows each in Perl?


use POSIX;
my $test = "";
my $elements = scalar(@array);
my $tablecount = ($elements / 100);
my $tblnum = ceil($tablecount);
my @hundred;
 foreach $test (@array) {
   until ($tblcnt == $tblnum){
   @hundred(@array, 0, 99);
   print qq~<table id="$tblcnt"><tr><td>~;
     foreach $test (@hundred){
     print qq~<tr><td>$test</td></tr>~;
     }
   print qq~</table>~;
   $tblcnt++;
   }
}

UG!!! I am trying to learn this but, I just cannot get it right!!!

I am trying to dynamically generate "x" number of html tables filled with up to 100 lines of data each.

I can get the table count needed, I can loop, I can add but, one thing is for sure: I CANNOT WRITE CODE.

Here is the result wanted:

1- I have 1027 lines of data from an array.

2- I want my script to make 11 html tables with up to 100 lines each. ((Layers actually) which by default will be not visible via css. That way I can do some show hide on the tables like a "next prev" navigation. I don't need help with the cross browser css.)

3- IF there is a better way, a method that I can comprehend anyhow, other than using visible= method, please elaborate!

I gave up trying to have Perl make pages of 100 with "next prev" links to the data so I resorted to using css javascript onclick=yadayada to "display the data".

I thought it would be easier to shift off 100 lines of the array in a loop and put the lines in their own html tables. Not.

I have failed miserably and need assistance.


Solution

  • my $cnt = 0;
    
    while (@array) {
        my @rows = splice @array, 0, 100;
        print qq(<table id="t$cnt">\n);
        for my $row (@rows) {
            print "<tr><td>$row</td></tr>\n";
        }
        print "</table>\n";
        ++$cnt;
    }
    

    You may want to use HTML::Table for generating HTML.