Solved

array elements in  TABLE rows

Posted on 2001-06-07
12
210 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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
 

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

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 …
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

815 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

11 Experts available now in Live!

Get 1:1 Help Now