Solved

array elements in  TABLE rows

Posted on 2001-06-07
12
207 Views
Last Modified: 2010-03-05
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
Comment
Question by:tmccar10
  • 6
  • 6
12 Comments
 
LVL 8

Expert Comment

by:bebonham
ID: 6163569
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
 
LVL 8

Expert Comment

by:bebonham
ID: 6163570
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
 

Author Comment

by:tmccar10
ID: 6163598
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
 
LVL 8

Expert Comment

by:bebonham
ID: 6163705
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
 

Author Comment

by:tmccar10
ID: 6164337
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
 

Author Comment

by:tmccar10
ID: 6164402
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

by:tmccar10
ID: 6164506
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
 
LVL 8

Expert Comment

by:bebonham
ID: 6165398
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
 
LVL 8

Accepted Solution

by:
bebonham earned 50 total points
ID: 6166059
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
 

Author Comment

by:tmccar10
ID: 6195637
I used this (which I couldn't get to work) with some other ideas and pretty much got what I needed

Thanks
0
 
LVL 8

Expert Comment

by:bebonham
ID: 6197063
sorry.
0
 

Author Comment

by:tmccar10
ID: 6197252
Hey, I appreciate the help. I suspect that I was doing something wrong.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now