Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

capturing terminal directory relative path?

by Anonymous Monk
on Mar 07, 2010 at 15:45 UTC ( [id://827245]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

Let's say I am traversing a directory:

/home/BORK/work/test-backfills/cp_test /home/BORK/work/test-backfills/cp_test/82468 /home/BORK/work/test-backfills/cp_test /home/BORK/work/test-backfills/cp_test/82480 /home/BORK/work/test-backfills/cp_test /home/BORK/work/test-backfills/cp_test/82485 /home/BORK/work/test-backfills/cp_test /home/BORK/work/test-backfills/cp_test/82490 /home/BORK/work/test-backfills/cp_test /home/BORK/work/test-backfills/cp_test/82767 /home/BORK/work/test-backfills/cp_test /home/BORK/work/test-backfills/cp_test/82802 /home/BORK/work/test-backfills/cp_test
I need to isolate the terminal directory path (in this case, 82468, 82480, 82485, etc) because they are ids that I need to insert into xml documents. I've been looking at the various file modules in cpan, but I can't figure out what to use. Maybe File::Spec? Any examples would be much appreciated.

Replies are listed 'Best First'.
Re: capturing terminal directory relative path?
by liverpole (Monsignor) on Mar 07, 2010 at 16:09 UTC
    What you probably want is File::Basename.  It's in the core already (ie. no need to install it).

    Try:

    use strict; use warnings; use File::Basename; my $base = basename("/home/BORK/work/test-backfills/cp_test/82802"); # Note that $base is now '82802'

    s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/
Re: capturing name of deepest directory in a tree structure.
by Corion (Patriarch) on Mar 07, 2010 at 17:17 UTC

    sort your strings, and then compare them using index. Potentially split them into their parts:

    /home/BORK/work/test-backfills/cp_test /home/BORK/work/test-backfills/cp_test /home/BORK/work/test-backfills/cp_test /home/BORK/work/test-backfills/cp_test/82468 /home/BORK/work/test-backfills/cp_test/82480
Re: capturing terminal directory relative path?
by bichonfrise74 (Vicar) on Mar 07, 2010 at 16:46 UTC
    I agree that File::Basename is the robust way to do what you need. But maybe the below code is something you might want to consider as well.
    #!/usr/bin/perl use strict; while (<DATA>) { print + (split( /\// ))[-1] if /\d+/; } __DATA__ /home/BORK/work/test-backfills/cp_test /home/BORK/work/test-backfills/cp_test/82468 /home/BORK/work/test-backfills/cp_test /home/BORK/work/test-backfills/cp_test/82480 /home/BORK/work/test-backfills/cp_test /home/BORK/work/test-backfills/cp_test/82485 /home/BORK/work/test-backfills/cp_test /home/BORK/work/test-backfills/cp_test/82490 /home/BORK/work/test-backfills/cp_test /home/BORK/work/test-backfills/cp_test/82767 /home/BORK/work/test-backfills/cp_test /home/BORK/work/test-backfills/cp_test/82802 /home/BORK/work/test-backfills/cp_test

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://827245]
Approved by moritz
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2024-04-20 01:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found