To add to Chad's last email, the following will cater for spaces and special (allowed) characters in the file name

$s1 = "c:\\temp\\bola\\temilola\\abolan-lef o_o.pl";
 
$s1 =~/\\([\w\._-\s]+)$/g;

print $1;

Tony B. Okusanya
Distributed Technology Group


"Live Life By Design And Not From Crisis to Crisis"



"Chad I. Uretsky" <[EMAIL PROTECTED]>
Sent by: [EMAIL PROTECTED]

04/21/2005 03:03 PM

To
"'[email protected]'" <[email protected]>
cc
Subject
RE: Filename pull





try:
 
$s1 = "c:\\temp\\foo.pl";
 
$barename = ($s1 =~ /\\(\w+\.\w+)$/)[0];
 
 
 
-----Original Message-----
From:
[EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of [EMAIL PROTECTED]
Sent:
Thursday, April 21, 2005 1:59 PM
To:
[email protected]
Subject:
Filename pull


Hey, wizards.


I've got this problem: pulling the bare filename off of a fully pathed string, using split. The tricky part is getting the last element in the array when I don't know how many members are in it. Example:


$s1 = "c:\\temp\\foo.pl";

$barename = (split( /\\/, $s1)[2];  # $barename gets "foo.pl"


Now obviously this works as far as it goes, but what I should put inside those square brackets to make sure I always grab the filename, even in varied cases like these


$s2 = "c:\\windows\\fee\\fie\\fo\\fum\\foo.pl";

$s3 = "c:\\base_foo.pl";


is the question.  If it was a regular array, I know I could get it using $#, like this:


@x = ("C:", "windows", "fee", "fum", "foo.pl");

$barename = $x[$#x];


Do I have to go to an intermediate step, assigning the output of split() to an array like @x? Or does someone have handy a regexp to do the dirty work?


Thanks!


Deane
_______________________________________________
ActivePerl mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

------------------------------------------------------------------------------
Electronic Privacy Notice. This e-mail, and any attachments, contains information that is, or may be, covered by electronic communications privacy laws, and is also confidential and proprietary in nature. If you are not the intended recipient, please be advised that you are legally prohibited from retaining, using, copying, distributing, or otherwise disclosing this information in any manner. Instead, please reply to the sender that you have received this communication in error, and then immediately delete it. Thank you in advance for your cooperation.
==============================================================================

_______________________________________________
ActivePerl mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to