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:
Stupid tables =0)

 



etheracide
journeyman

Dec 10, 2004, 1:36 AM

Post #1 of 5 (318 views)
Stupid tables =0) Can't Post

I've bumped myself down to "beginner" because of this problem hahaha

I have a directory called "categories" with named folders in it. Now the number of folders in the categoies directory will vary. I have a table with two columns...and a name of a folder in that directory is going to be printed into each <td>. Obviously the number of rows will depend upon the number of total folders in the categories directory.

Obviously I do not want to hardcode the table because the numbe of folders in the directory will vary. I want the first two folders to be printed in the two colums of the first row and so forth and so on. But if there is an odd number of folders when the script is run I would like "colspan=2" in the <td> of the last tablerow so that everything is still nicely aligned.

I've tried so many things but none of them even close to correct..well as far as I could tell haha

Here is what I have just to get the folders:

opendir(HOME, "./categories");
@folders = readdir (HOME);
closedir(HOME);

But nothing I tried after that would print the table as I need it to be.

If you need any more info, please let me know. Much thanks in advance for anyone that can offer help.


davorg
Thaumaturge / Moderator

Dec 10, 2004, 2:10 AM

Post #2 of 5 (316 views)
Re: [etheracide] Stupid tables =0) [In reply to] Can't Post

Something like this would appear to do the job. Try playing with the number of items in @folders.


Code
#!/usr/bin/perl 

use strict;
use warnings;

my @folders = qw(one two three four five);

print "<table>\n";

while (my @row = splice @folders, 0, 2) {
print "<tr>\n";
if (@row == 2) {
print map { "<td>$_</td>" } @row;
} else {
print qq(<td colspan="2">$row[0]</td>);
}
print "\n</tr>\n";
}

print "</table>\n";


--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


etheracide
journeyman

Dec 10, 2004, 8:54 AM

Post #3 of 5 (312 views)
Re: [davorg] Stupid tables =0) [In reply to] Can't Post

Thanks for the help! I simply removed the qw you'd set for @folders since I already had that esstablished. Another quick question, obviously I this is going to print a <td> for "." and ".." and normally I use the following after the print statement:

unless ($_ =~ /^\.+$/)

to remove the parent folders. But I can't get it to work using this fashion. Yes, I know..I DO need my hand held each step of the way haha


davorg
Thaumaturge / Moderator

Dec 10, 2004, 9:10 AM

Post #4 of 5 (311 views)
Re: [etheracide] Stupid tables =0) [In reply to] Can't Post

Easiest solution is probably to filter @folders before you start to use it.


Code
@folders = grep { /[^.]/ } @folders;


This removes from @folders anything that consists of nothing but dots.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


etheracide
journeyman

Dec 10, 2004, 9:56 AM

Post #5 of 5 (310 views)
Re: [davorg] Stupid tables =0) [In reply to] Can't Post

Thanks again Dave. *bows down* Such a simple solution. Why do i do this again? hehe And I do appreciate your not bashing me while helping.

 
 


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

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