CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
CGI.pm and Tables

 



SirAnvil
Deleted

Mar 3, 2001, 8:59 AM

Post #1 of 5 (451 views)
CGI.pm and Tables Can't Post

I was wondering, I read all documents I could find on CGI.pm and can't seem to find what I'm looking for. I have a flat file db like so
word 1 1
word_2 10 10
something 100 100

ect.... I need a loop that would draw a table and place each one on it's own row and have column names for each column

words numbers more_numbers
word 1 100
words 1 1000

Any ideas?

SirAnvil
www.christiangamers.org


rGeoffrey
User / Moderator

Mar 5, 2001, 7:06 PM

Post #2 of 5 (434 views)
Re: CGI.pm and Tables [In reply to] Can't Post

Here is a solution that does not need CGI.pm...


Code
#!/usr/local/bin/perl 

my $datafile = 'buildtable.in';

print ("\n<table><tr>\n",
(map {"<th>$_</th>\n"} ('words', 'numbers', 'more numbers')),
"</tr>");

open (DATA, $datafile) or die "reading $datafile: $!\n";

while (<DATA>) {
chomp;
print ("<tr>\n", (map {"<td>$_</td>\n"} split (" ")), "</tr>");
}

close DATA;

print ("</table>\n\n");

As a bonus note that the split will work on any amount of white space, not just a single space, throught the magic of split.

--
Sun Sep 9, 2001 - 1:46:40 GMT, a very special second in the epoch. How will you celebrate?


SirAnvil
Deleted

Mar 8, 2001, 10:01 AM

Post #3 of 5 (428 views)
Re: CGI.pm and Tables [In reply to] Can't Post

I got two questions now.
1. The code works however it takes every letter and ever number and places it in it's now box. such as my name will would be w i l l. How can we correct that?
2. Anyway I could get a detailed description on how this code works? It threw me off as I'm still new to perl and am studying intensly. Thanx in advance

SirAnvil
www.christiangamers.org


japhy
Enthusiast

Mar 8, 2001, 11:48 AM

Post #4 of 5 (426 views)
Re: CGI.pm and Tables [In reply to] Can't Post

The code uses map(). It's like a for loop, except it returns things. The problem you have is because you change:


Code
split(" ")

to


Code
split("")

There should be a space in between the quotes.

Jeff "japhy" Pinyan -- accomplished hacker, teacher, lecturer, and author


rGeoffrey
User / Moderator

Mar 8, 2001, 9:23 PM

Post #5 of 5 (421 views)
Re: CGI.pm and Tables [In reply to] Can't Post

To help see what is happening, consider this alternate while loop...


Code
while (<DATA>) { 
chomp ($_); #remove the "\n" from the end of each line
my @parts = split (" ", $_);
print "<tr>\n";
foreach my $cell (@parts) {
print "<td>$cell</td>\n";
}
print "</tr>";
}

There are several changes including...

The map has been expanded into a foreach.

The print has been divided into three parts.

The $_ is now shown for chomp and split, if you don't give something they default to $_ so the original version could save a few characters.

The map used $_ while the foreach names its variable $cell for clarity.

An array @parts holds the results from split and then passes them to the foreach loop.

--
Sun Sep 9, 2001 - 1:46:40 GMT, a very special second in the epoch. How will you celebrate?

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives