Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I do this? 2

Status
Not open for further replies.

rcapilli

Technical User
Jan 30, 2006
2
US
By doing this

my $picture = $query->param('Picture');
my $directory = $query->param('directory');

I get my directory and all the pictures in the Directory, but I also get "." and ".." in my $picture variable. How do I do this w/o getting the "." and ".."?

I assume the $Picture variable is looking in the Directory as:

.
..
Picture1.jpg
Picture2.jpg
Picture3.jpg
Picture4.jpg



I only want:

Picture1.jpg
Picture2.jpg
Picture3.jpg
Picture4.jpg
 
You could remove the offending lines from the variable:
[tt]
$picture=~s/^[.\n]*//;
[/tt]
 
one way:

Code:
opendir(DIR,$directory) or die "$!";
my @pictures = grep {/\.jpg$/i} readdir DIR;
close (DIR);

another way:

Code:
chdir($directory) or die "$!";
my @pictures = <*.jpg>;
 
I like this idea:

opendir(DIR,$directory) or die "$!";
my @pictures = grep {/\.jpg$/i} readdir DIR;
close (DIR);

But that only get's ".jpg" files. But I have others in there as well such as ".JPG" ".mpg" ".avi" etc... Can I add this to the code above?

 
Well, the motto is "There's more than one way to do it" :) ...

I guess that your picture CGI parameter is an array reference which points to an array of file names.

It probably wouldn't have any effect in this case, but regular expressions are supposed to be slower than direct comparisons.
Code:
my $picture = $query->param( "picture" );

for ( @$picture ) {
    my $file = $_;
    next if ( $file = '.' );
    next if ( $file = '..' );

    print qq{
        <h1>I got me a picher!!</h1>
        <p style="font-size: 96px;">It's name is $file</p>
    };
}

--
-- Ghodmode
 
A small correction to the prev entry.

CODE
my $picture = $query->param( "picture" );

for ( @$picture ) {
my $file = $_;
next if ( $file = '.' );
// shd be $file eq '.'

next if ( $file = '..' );
// shd be $file eq '..'

print qq{
<h1>I got me a picher!!</h1>
<p style="font-size: 96px;">It's name is $file</p>
};
}
 
star to vjcyrano. Absolutely right and I make that mistake all the time... [mad] doh!

--
-- Ghodmode
 
But that only get's ".jpg" files. But I have others in there as well such as ".JPG" ".mpg" ".avi" etc... Can I add this to the code above?

Code:
my @pictures = grep {/\.jpg|\.gif|\.mpg|\.avi$/i} readdir DIR;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top