Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

array elements in  TABLE rows

Posted on 2001-06-07
12
Medium Priority
?
217 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 150 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

Industry Leaders: 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!

Question has a verified solution.

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

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…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
Six Sigma Control Plans

610 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