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:
parsing filename

 



birdy
Novice

Mar 13, 2008, 8:42 AM

Post #1 of 4 (417 views)
parsing filename Can't Post

Hello,,

I need to travel a root directory and its sub directories to grab all.pdf files. I have the restriction of not being able to download modules on th eunix box accept for the stadard module. so using the following line of cod eto grab the list of files:

@filelist=`find $source_dir -name "*.pdf"`;

#print @filelist;
foreach $files(@filelist)
{
print "\n $files";
}

no wthe problem i sthat I need to strip th eFilename with all the dir and sub dir in fo.

As I am new to Perl having a hard time coding it out. Can anyone please help me with the code???


PGScooter
stranger

Mar 14, 2008, 11:58 PM

Post #2 of 4 (401 views)
Re: [birdy] parsing filename [In reply to] Can't Post

hi birdy,

so I think you're asking how you can convert a string such as 'blah/blah/nameofpdf.pdf"
to nameofpdf.pdf?

Is that what you're asking?

If so, regular expressions are your answer! if you want to do more research, look them up on google.


Code
$file='blah/blah/blah/wantthis.pdf'; 
if ($file=~/(\w*\.pdf)/) {
print $1;
}



A question if someone happens to see this.

Why doesn't this code work?


Code
$file='blah/blah/blah/wantthis.pdf'; 
if ($file=~/(\/\S*?pdf)/) {
print $1;
}


Doesn't this correctly say: "find a forward slash followed by as many nonwhitespace characters until pdf"? And since I'm using a non-greedy operator, shouldn't it correctly just spit back wantthis.pdf? What am I missing?

Hope that helps birdy,

Scott
The more you teach me, the more I learn. The more I learn, the more I teach.


birdy
Novice

Mar 17, 2008, 7:40 AM

Post #3 of 4 (389 views)
Re: [PGScooter] parsing filename [In reply to] Can't Post

thanks a ton!!! ya that exactly was my question ..thanks!!


PGScooter
stranger

Mar 17, 2008, 2:56 PM

Post #4 of 4 (382 views)
Re: [birdy] parsing filename [In reply to] Can't Post

if you plan on using perl in the future, you should definitely try to learn regexes!!! they are very powerful and can do pretty much any string manipulation you can think of.

To learn more about them, look up on google "regular expressions perl"

good luck!
The more you teach me, the more I learn. The more I learn, the more I teach.

 
 


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

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