Forum Moderators: coopster & phranque

Message Too Old, No Replies

var = last directory in path

perl

         

rcjordan

3:06 pm on May 7, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I need to work with the LAST directory in the path, no matter how long that path might be.

If the script is creating pages under "United_States/North_Carolina/Travel_Photos" I'd like to have just "Travel_Photos" broken out as a variable

or when in "United_States/North_Carolina/Travel_Photos/Raleigh" it would need
to return "Raleigh" as the variable.

I tried this

my $s="United_States/North_Carolina/Travel_Photos";
@arr = split("\/",$s);
$your_var= $arr[2];

But it seems to return the 3rd directory

ShawnR

4:13 pm on May 7, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Array indexes start from zero, so yes, $your_var= $arr[2]; will return the third element.

If you don't know how many element there are:

$your_var= $arr[-1]; # will return the last,
$your_var= $arr[-2]; # will return the second last, etc.

Shawn

rcjordan

5:00 pm on May 7, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



works fine. thanks!