Solved

array elements in  TABLE rows

Posted on 2001-06-07
12
208 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

932 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

13 Experts available now in Live!

Get 1:1 Help Now