in reply to Re: CGI problem help
in thread CGI problem help

Hi Monks,

Thanks for your reply. I did checked again and corrected few things and the table is created now. But I am facing a issue.

I am reading data from a file, processing it and then trying to put the data in table accordingly. Table has 40 rows and 3 columns. Below is the code:

#!C:\perl\bin\perl.exe use strict; use CGI; use CGI qw(:standard); use CGI::Carp qw(warningsToBrowser fatalsToBrowser); use HTML::Table; my $linecount=0; open(FILE,"details.txt")or die("Sorry can't open $!\n"); my $cgi=CGI->new; print $cgi->header; print $cgi->start_html("Test display page"); print $cgi->h3("Testing the output format"); my $table = new HTML::Table(-rows=>40, -cols=>3, -align=>'left', -border=>1, -width=>'30%', -padding=>5, -head=> ['col1','col2','col3']); OUTER: while(my $line = <FILE>) { chomp($line); my @field=split(/\s+/,$line); for(my $j=1;$j<=24;$j++) { $table->setCell($linecount, $j, $field[($j-1)]); } $linecount++; } close(FILE); print $table->getTable; print $cgi->end_html;

It's printing the columns but data's are not coming somehow. When run the code without html it displays the data. Please help to know where I am going wrong.

Thanks, sir_com

Replies are listed 'Best First'.
Re^2: CGI problem help
by CountZero (Bishop) on Sep 15, 2009 at 18:30 UTC
    I'm still guessing, but most likely your error is in:

    • my @field=split(/\s+/,$line); and split does not return the results you expect; or
    • there is something wrong with your for loop. You seem to expect 24 elements in each line of your file and you store these in @field but yet your table constructor is called with 3 rows as a parameter only. I know that HTML::Table will add extra cells as and when necessary, but still it is a bit strange.
    Have you tried adding a whole row at a time?
    $table->addRow(@field);
    Then all you need is the while loop, it replaces the whole for loop.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

      Hi CountZero,

      Thanks for your suggestion on this but I have checked that @field=split(/\s+/,$line); is working fine and returning the result as needed.

      Also,there is nothing worng in the for loop. mistakenly I typed 24 while posting it. actually it's 3 (as mentioned that there is 3 cols).

      I have also tried the same loop part in a script and it returns the data for every cell/field. Then I don't understand why I am not getting the smae in table here

      I haven't tried the addrow() function. Let me try that now.

      Thanks again. sir_com
      Hi CountZero,

      Thanks for pointing me to the addRow() function. Now, it's working fine. I too guess that something was going wrong with the for loop.

      Thanks sir_com