• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 218
  • Last Modified:

array elements in TABLE rows

I'd like to display each the elements of an array (@comments) in separate rows of a <table>.
The number of elements in the array is variable.

I've found stuff like this around,but  I can't seem to make it work. I'm just looking for a simple process--that maybe even I could understand ;-)

I'd appreciate any help

0
tmccar10
Asked:
tmccar10
  • 6
  • 6
1 Solution
 
bebonhamCommented:
here is how to do it using cgi:

use CGI ':standard';

my @array=qw(an array of random words that will be in a table);
my $table='';
foreach(@array)
{
$table.=Tr(td($_));
}
print header, start_html;
print table($table);




here is how to do it without CGI:


my @array=qw(an array of random words that will be in a table);
my $table='';
foreach(@array)
{
$table.="<TR><TD>$_</TD></TR>";
}
print "Content-type: text/html\n\n";
print "<TABLE>$table</TABLE>";


Bob

0
 
bebonhamCommented:
here is how to do it using cgi:

use CGI ':standard';

my @array=qw(an array of random words that will be in a table);
my $table='';
foreach(@array)
{
$table.=Tr(td($_));
}
print header, start_html;
print table($table);




here is how to do it without CGI:


my @array=qw(an array of random words that will be in a table);
my $table='';
foreach(@array)
{
$table.="<TR><TD>$_</TD></TR>";
}
print "Content-type: text/html\n\n";
print "<TABLE>$table</TABLE>";


Bob

0
 
tmccar10Author Commented:
Bob-
I do need the CGI solution.

The array already exists as @comments.
Would I sustitute that for my@array or make is my@comments? Or skip that line and use @comments in the foreach loop.

Is there significance to the "my" in my@array, my$table?

Thanks
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
bebonhamCommented:
no, my is just like var in javascript or dim in vb

it just is a variable scoping tool.

you can leave out the my's entirely, here is the fixed script:


use CGI ':standard';

$table='';  #make $table empty because we are appending to it
foreach(@comments)
{
$table.=Tr(td($_));
}
print header, start_html;
print table($table);




now____

the above should work fine for you, but here is another alternative...the deal with this it's a subroutine, so all you do is call it with your array

print makeTable(@comments);  # here you call the sub, but the sub doesn't start till below


sub makeTable
{
$table='';  #make $table empty because we are appending to it
foreach(@_)
{
$table.=Tr(td($_));
}
print header, start_html;
return table($table);
}



Bob
0
 
tmccar10Author Commented:
Sorry Bob,

Could you look at this one more time.
I still can't get it to work.

Thom
##################
I'm getting this message:

Undefined subroutine &main::td called at /ford/thishost/u/mso/www.80/fcsd/psl/namer/webtraining/cgi-bin/isoout2.cgi line 49, chunk 1.

###################
Using this code:

#!/usr/local/bin/perl
use CGI qw(:standard :shortcuts);    
                       use Fcntl;
                          $|++;
                       $query=new CGI;

 MAIN:
 {
 print "Content-type: text/html\n\n";
print "<head><title>Approve Page</title></head>\n";
print "<html><body bgcolor=\"#FFFFFF\">\n";  
#Open text file where tab-delimited input is sent
$path="../data/ee.txt";

@docnum=$query->param('docnum');

                       ## First we open the file and read the file
                       ## into a hash array, such that the 1st column is the
                       ## key to the array.
open (TXTFILE,$path)||die "cannot open $path\n";
                       ## Shared lock (for reading)
                       #flock(TXTFILE, 1) || die "can't flock $path: $!";

while(<TXTFILE>){

s/\r\n//g;               ## Remove the Ctrl + M character
next if /^$/;          ## Skip blank lines
@items=split(/\t/,$_);

$items{$items[0]}=$_;
@comments=split(/#/,@items[1]);
$table='';  
      foreach(@comments)
      {
      $table.=Tr(td($_));
      }

      print header, start_html;
      print table($table);

close(TXTFILE) || die "can't close $path: $!";                                
                       
print "</body></html>";

}

}
0
 
tmccar10Author Commented:
Sorry Bob,

Could you look at this one more time.
I still can't get it to work.

Thom
##################
I'm getting this message:

Undefined subroutine &main::td called at /ford/thishost/u/mso/www.80/fcsd/psl/namer/webtraining/cgi-bin/isoout2.cgi line 49, chunk 1.

###################
Using this code:

#!/usr/local/bin/perl
use CGI qw(:standard :shortcuts);    
                       use Fcntl;
                          $|++;
                       $query=new CGI;

 MAIN:
 {
 print "Content-type: text/html\n\n";
print "<head><title>Approve Page</title></head>\n";
print "<html><body bgcolor=\"#FFFFFF\">\n";  
#Open text file where tab-delimited input is sent
$path="../data/ee.txt";

@docnum=$query->param('docnum');

                       ## First we open the file and read the file
                       ## into a hash array, such that the 1st column is the
                       ## key to the array.
open (TXTFILE,$path)||die "cannot open $path\n";
                       ## Shared lock (for reading)
                       #flock(TXTFILE, 1) || die "can't flock $path: $!";

while(<TXTFILE>){

s/\r\n//g;               ## Remove the Ctrl + M character
next if /^$/;          ## Skip blank lines
@items=split(/\t/,$_);

$items{$items[0]}=$_;
@comments=split(/#/,@items[1]);
$table='';  
      foreach(@comments)
      {
      $table.=Tr(td($_));
      }

      print header, start_html;
      print table($table);

close(TXTFILE) || die "can't close $path: $!";                                
                       
print "</body></html>";

}

}
0
 
tmccar10Author Commented:
Sorry Bob,

Could you look at this one more time.
I still can't get it to work.

Thom
##################
I'm getting this message:

Undefined subroutine &main::td called at /ford/thishost/u/mso/www.80/fcsd/psl/namer/webtraining/cgi-bin/isoout2.cgi line 49, chunk 1.

###################
Using this code:

#!/usr/local/bin/perl
use CGI qw(:standard :shortcuts);    
                       use Fcntl;
                          $|++;
                       $query=new CGI;

 MAIN:
 {
 print "Content-type: text/html\n\n";
print "<head><title>Approve Page</title></head>\n";
print "<html><body bgcolor=\"#FFFFFF\">\n";  
#Open text file where tab-delimited input is sent
$path="../data/ee.txt";

@docnum=$query->param('docnum');

                       ## First we open the file and read the file
                       ## into a hash array, such that the 1st column is the
                       ## key to the array.
open (TXTFILE,$path)||die "cannot open $path\n";
                       ## Shared lock (for reading)
                       #flock(TXTFILE, 1) || die "can't flock $path: $!";

while(<TXTFILE>){

s/\r\n//g;               ## Remove the Ctrl + M character
next if /^$/;          ## Skip blank lines
@items=split(/\t/,$_);

$items{$items[0]}=$_;
@comments=split(/#/,@items[1]);
$table='';  
      foreach(@comments)
      {
      $table.=Tr(td($_));
      }

      print header, start_html;
      print table($table);

close(TXTFILE) || die "can't close $path: $!";                                
                       
print "</body></html>";

}

}
0
 
bebonhamCommented:
hmmm....doesn't like td() huh...

welll could be something with CGI ':standard'

not sure...let's get around it ...what'dya say?

#!/usr/local/bin/perl
use CGI qw(:standard :shortcuts);    
                      use Fcntl;
                         $|++;
                      $query=new CGI;

MAIN:
{
print "Content-type: text/html\n\n";
print "<head><title>Approve Page</title></head>\n";
print "<html><body bgcolor=\"#FFFFFF\">\n";  
#Open text file where tab-delimited input is sent
$path="../data/ee.txt";

@docnum=$query->param('docnum');

                      ## First we open the file and read the file
                      ## into a hash array, such that the 1st column is the
                      ## key to the array.
open (TXTFILE,$path)||die "cannot open $path\n";
                      ## Shared lock (for reading)
                      #flock(TXTFILE, 1) || die "can't flock $path: $!";

while(<TXTFILE>){

s/\r\n//g;               ## Remove the Ctrl + M character
next if /^$/;          ## Skip blank lines
@items=split(/\t/,$_);

$items{$items[0]}=$_;
@comments=split(/#/,@items[1]);
$table='';  
     foreach(@comments)
     {
     $table.="<TR><TD>$_</TD></TR>);
     }
     print "<TABLE>$table</TABLE>";

close(TXTFILE) || die "can't close $path: $!";                                
                     
print "</body></html>";

}

}



hope that does it for ya!

good luck!

Bob
0
 
bebonhamCommented:
my bad....

I didn't notice you were using it in another loop using $_


try this:


#!/usr/local/bin/perl
use CGI qw(:standard :shortcuts);    
                      use Fcntl;
                         $|++;
                      $query=new CGI;

MAIN:
{
print "Content-type: text/html\n\n";
print "<head><title>Approve Page</title></head>\n";
print "<html><body bgcolor=\"#FFFFFF\">\n";  
#Open text file where tab-delimited input is sent
$path="../data/ee.txt";

@docnum=$query->param('docnum');

                      ## First we open the file and read the file
                      ## into a hash array, such that the 1st column is the
                      ## key to the array.
open (TXTFILE,$path)||die "cannot open $path\n";
                      ## Shared lock (for reading)
                      #flock(TXTFILE, 1) || die "can't flock $path: $!";

while(<TXTFILE>){

s/\r\n//g;               ## Remove the Ctrl + M character
next if /^$/;          ## Skip blank lines
@items=split(/\t/,$_);

$items{$items[0]}=$_;
@comments=split(/#/,@items[1]);
$table='';  
     foreach $comment(@comments)
     {
     $table.=Tr(td($comment));
     }

     print header, start_html;
     print table($table);

close(TXTFILE) || die "can't close $path: $!";                                
                     
print "</body></html>";

}

}


sometimes I just miss the obvious.

Bob
0
 
tmccar10Author Commented:
I used this (which I couldn't get to work) with some other ideas and pretty much got what I needed

Thanks
0
 
bebonhamCommented:
sorry.
0
 
tmccar10Author Commented:
Hey, I appreciate the help. I suspect that I was doing something wrong.
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 6
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now