Forum Moderators: coopster & phranque

Message Too Old, No Replies

Filename filtering

Need help, please...

         

adni18

3:26 pm on Feb 5, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



All right. First off, this is a script that should open a directory, display only txt and jpeg files, filter out files with more than and less than one period, and display them in a table, with an alternating color assigned to each row. However, the filetype filtering is not working. Please help.

#!/usr/bin/perl

use CGI;

print "Content-type:text/html\n\n";

print "<TABLE>\n";

@filetypes=("txt","jpg");

@realfiles;

opendir(IMD, "accounts/carniojack424/") ¦¦ die("Cannot open directory");
@thefiles= readdir(IMD);
closedir(IMD);

foreach $ind (@thefiles)
{
my $count = ($ind =~ tr/\.//);

my $doneyet=0;

my @ext=split($ind, /\./);

foreach(sort @filetypes)
{
if($_ eq $ext[1])
{
$doneyet=1;
}
}

unless($ind eq "." ¦¦ $ind eq ".." ¦¦ $count >= 2 ¦¦ $doneyet == 0 ¦¦ $count <=0)
{
@realfiles=(@realfiles, $ind."<br>");
}

}
@realfiles=sort (@realfiles);
@color=("\#FFE5A6","\#FFFFFF");
$currColor=0;

foreach(sort @realfiles)
{
$done=0;
if($currColor == 0 && $done==0)
{
$currColor=1;
$done=1;
}
if($currColor == 1 && $done==0)
{
$currColor=0;
$done=1;
}
print "\n\t<TR>\n\t\t<TD style=\"background-color:".$color[$currColor]."\">\n\t\t\t" . $_ . "\n\t\t</TD>\n\t</TR>\n";
}

print "\n</TABLE>";

SeanW

6:39 pm on Feb 5, 2005 (gmt 0)

10+ Year Member



Do you even get a list of files in the directory? You're opening up the dir with a relative path, and as a CGI, I think it'll fail.

For performance reasons I'd switch your loop around to check one regexp against all files, then change regexps, rather than the other way around. There's an initial hit on the first regexp since it has to be compiled.

It also looks like you're stripping out periods from the file, then trying to split on periods.

I'd put in a bunch of print statements, or run through the perl debugger to see where things are going wrong...

Sean

adni18

9:17 pm on Feb 5, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Actually, I just discovered the problem, and it's none of the above.

my @ext=split($ind, /\./)

should be

my @ext=split(/\./, $ind)

I wasn't stripping out periods, just stripping out files with 0 or >= 2 periods.

rocknbil

7:28 pm on Feb 7, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Here, use this

if ($done == 0) {
$currColor = ($currColor==0)?1:0;
$done = 1;
}

instead of this

if($currColor == 0 && $done==0)
{
$currColor=1;
$done=1;
}
if($currColor == 1 && $done==0)
{
$currColor=0;
$done=1;
}

:-)

wruppert

2:39 pm on Feb 9, 2005 (gmt 0)

10+ Year Member



Or...

my $rows = 0;
foreach ... {
$color = $rows++ % 2;
...
}

You can rotate through as many colors as you want:


my $rows = 0;
my $color_count = 2;
foreach ... {
$color = $rows++ % $color_count;
...
}

You can have 3 rows one color then the next three rows another color:


my $rows = 0;
foreach ... {
use integer;
$color = $rows++/3 % 2;
...
}