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

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Beginner:
Print to a Table



Apr 29, 2001, 6:04 AM

Post #1 of 3 (368 views)
Print to a Table Can't Post

Hi All,

A while back, rGeoffrey helped me with a sort and a print from my database and i would like to see if I could take it one step further.

As you can see, he gave me the code to take all the rows in my database and give me the link to each 'Category' even tho there would be multiple listings under each one.

  sub do_cat_update 
open(ARRAY, "../Data_files/") || &eg_error(array);
@data = <ARRAY>;
my %hash;
foreach (@data) {
my $category = (split ('\|', $_))[1];
$hash{$category} = 1 unless (exists ($hash{$category}));
my @links = map { '<a href="%%sc_link%%&product=' . "$_\">$_</a>\n" } sort keys %hash;

open(CONFIG, ">../Library/cat.cfg") || &eg_error(cat_cfg);
print CONFIG "@links</font>\n";


What I would like to do now is print them to a table. I have the basic code down to do this but my question is let's say I have 9 categories, how could I print that to three rows of three categories.


I have tried numerous combinations but can't get close and this board is always a good source when you are stumped...


User / Moderator

Apr 30, 2001, 10:02 PM

Post #2 of 3 (353 views)
Re: Print to a Table [In reply to] Can't Post

After you have your array of things to print, you can use this...

my @array = qw (apple banana pear kiwi orange tangerine grapefruit 
watermelon strawberry blueberry blackberry);

&make_table (3, 'Fruits', @array);

sub make_table
my ($width, $message, @images) = @_;
my $columns = (@images > $width) ? $width : scalar (@images);

print "<table><tr><th colspan=\"$columns\">$message</th></tr>\n";

while (@images) {
print ("<tr>",
(map { "<td>$_</td>" } splice (@images, 0, $columns)),

print "</table>\n";

The splice is used to grab the parts in $width chunks for each row. And in the last row it places the leftovers. As an added bonus if you set width higher than the number of elements in the array, then it will reduce the colspan in the header row to the number of columns you actually need.

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


May 2, 2001, 1:11 AM

Post #3 of 3 (342 views)
Re: Print to a Table [In reply to] Can't Post

Many thanks again rGeoffrey,

As usual, it worked like a charm.

I didn't need the <th> thingy, but after getting the snippet working, I've made that another working variable in the script...

Again, Thanks for your time



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

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