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: Intermediate:
Build a filename array with addtl info...

 



S_Shrum
User

Nov 12, 2001, 3:22 PM

Post #1 of 7 (1189 views)
Build a filename array with addtl info... Can't Post

I am attempting to build an array that will contain 2 columns: path and filename.

PATH is specified by the user and FILENAME is the files in the PATH. The finished array will look sort of like this:


Code
     soaring/photos|picture1.jpg 
soaring/photos|picture2.jpg
...

I need it structured this way for consistancy within the script as I am also handling what I call TOC (table of contents dbs). The TOC dbs are structured like:


Code
     MYPICTURES.TOC 
path|filename
soaring/photos|picture2.jpg
family/photos|beachday.jpg
...

This will allow for customized photo albums and will allow the user to pull photos from other directories on their websites.

Back to my problem:

While I know how to dump the contents of a directory into an array by themselves, the thing I can't seem to figure out is how to create an array of an array where I use READDIR with it. I tried:


Code
@filenames = [ @{[$input->param('path')]}, readdir DIR ];

...but I believe that the syntax is incorrect for this sort of method (besides, it fails to run anyways).

Any ideas?

Sean Shrum
sean@shrum.net
http://www.shrum.net/programming - Free Perl Scripts
http://www.shrum.net/soaring - Sailplane Soaring and Combat


mhx
Enthusiast / Moderator

Nov 12, 2001, 8:00 PM

Post #2 of 7 (1186 views)
Re: Build a filename array with addtl info... [In reply to] Can't Post

This should work, I hope it does what you expect Wink:

Code
opendir DIR, $input->param('path') or die "cannot open ".$input->param('path').": $!\n"; 
my @filenames = map [$input->param('path'), $_], readdir DIR;
closedir DIR;

-- Marcus


Code
s$$ab21b8d15c3d97bd6317286d$;$"=547269736;split'i',join$,,map{chr(($*+= 
($">>=1)&1?-hex:hex)+0140)}/./g;$"=chr$";s;.;\u$&;for@_[0,2];print"@_,"



S_Shrum
User

Nov 12, 2001, 8:39 PM

Post #3 of 7 (1183 views)
Re: Build a filename array with addtl info... [In reply to] Can't Post

That's the stuff...BUT, now GREP isn't working.

Can GREP be used on a array of arrays? I assume that it can I just don't know the syntax. This is what I currently have that doesn't seem to be working:


Code
if ( $input->param('extensions') ) { @filenames = grep /@{[$input->param('extensions')]}$/i, @filenames; }

Thanks for the help.

Sean Shrum
sean@shrum.net
http://www.shrum.net/programming - Free Perl Scripts
http://www.shrum.net/soaring - Sailplane Soaring and Combat


mhx
Enthusiast / Moderator

Nov 12, 2001, 10:46 PM

Post #4 of 7 (1180 views)
Re: Build a filename array with addtl info... [In reply to] Can't Post

So what's the 'extensions' parameter supposed to be? Please give me an example...

-- Marcus


Code
s$$ab21b8d15c3d97bd6317286d$;$"=547269736;split'i',join$,,map{chr(($*+= 
($">>=1)&1?-hex:hex)+0140)}/./g;$"=chr$";s;.;\u$&;for@_[0,2];print"@_,"



S_Shrum
User

Nov 12, 2001, 11:33 PM

Post #5 of 7 (1178 views)
Re: Build a filename array with addtl info... [In reply to] Can't Post

Opps, good point.

$input->param('extensions') is defined as "gif|jpg|png".

The pipes are not to primarily to seperate but to set up a OR condition.

Sean Shrum
sean@shrum.net
http://www.shrum.net/programming - Free Perl Scripts
http://www.shrum.net/soaring - Sailplane Soaring and Combat


mhx
Enthusiast / Moderator

Nov 13, 2001, 2:33 AM

Post #6 of 7 (1174 views)
Re: Build a filename array with addtl info... [In reply to] Can't Post

This should work for you:

Code
#!/usr/bin/perl -w 
use strict;
use Data::Dumper;
use CGI qw/-no_debug/;

my $input = new CGI;

my @filenames = (
['soaring/photos', 'picture1.jpg' ],
['soaring/photos', 'picture2.txt' ],
['cars/hello/photos', 'picture3.png' ],
['soaring/photos', 'picture4._jpg'],
['abc/photos', 'picture5.tif' ],
);

$input->param( extensions => 'gif|png|tif|jpg' );

@filenames = grep $_->[1] =~ /\.(?:@{[$input->param('extensions')]})$/i,
@filenames if $input->param('extensions');

print Dumper( \@filenames );

-- Marcus


Code
s$$ab21b8d15c3d97bd6317286d$;$"=547269736;split'i',join$,,map{chr(($*+= 
($">>=1)&1?-hex:hex)+0140)}/./g;$"=chr$";s;.;\u$&;for@_[0,2];print"@_,"



S_Shrum
User

Nov 13, 2001, 2:09 PM

Post #7 of 7 (1163 views)
Re: Build a filename array with addtl info... [In reply to] Can't Post

So far so good...I thought I could use the same syntax (with some minor changes) for doing NOT conditions but it's not working. Just to make things a bit clearer, I am using the following filename conventions for the photo files:


Code
     filename.jpg           ( the actual photo ) 
~filename.jpg ( the photo thumbnail )
~filename.jpg.caption ( the thumbnail caption )

This is what I made to do the "not starting with ~" GREP, but it's not working at all:


Code
@filenames = grep $_->[1] =~ !/^(?:@{[$input->param('pre')]})/i, @filenames if $input->param('pre');

What should be left is an array of filenames that end with what is defined in $input->param('extensions') ( this part is working) but do not start with what is defined in the $input->param('pre') (which is not working). 'pre' in my case being "~".

While I'm asking, could you also send the syntax for doing a 'contains' and 'omit' GREP calls on just the filename (column 1). This way, script callers can filter the files that are displayed.

Thanks for all the help.

Sean Shrum
sean@shrum.net
http://www.shrum.net/programming - Free Perl Scripts
http://www.shrum.net/soaring - Sailplane Soaring and Combat

 
 


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

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