Getting Text Value , in Single line

G

George Kinley

I have a String, which is long Path of a file
for example "c:\kl\w\e\l\pol....\FileName.EXT" now I want to have
"FileName" out of this Sting, I can very well do it by writing a few l
lines of code, can it be done in single line,

--
 
J

Jürgen Exner

George said:
I have a String, which is long Path of a file
for example "c:\kl\w\e\l\pol....\FileName.EXT" now I want to have
"FileName" out of this Sting, I can very well do it by writing a few l
lines of code, can it be done in single line,

use File::Basename;
($name,$path,$suffix) = fileparse($fullname,@suffixlist);

Ok, that's two lines, but correctness is more important then shortness, even
in Perl golf.

jue
 
M

Mladen Gogala

I have a String, which is long Path of a file
for example "c:\kl\w\e\l\pol....\FileName.EXT" now I want to have
"FileName" out of this Sting, I can very well do it by writing a few l
lines of code, can it be done in single line,

$ pwd
/home/mgogala/Business/work
$ pwd|perl -ne '$A=(split(/\//))[-1]; print $A;'
work
$
 
T

Tony Muler

George said:
I have a String, which is long Path of a file
for example "c:\kl\w\e\l\pol....\FileName.EXT" now I want to have
"FileName" out of this Sting, I can very well do it by writing a few l
lines of code, can it be done in single line,

Looks like you want anything between the last path separator
and the last dot in the full path?

my $full = 'c:\kl\w\e\l\pol....\FileName.EXT';
my ($base) = $full =~ /.*\\(.*)\..*/;

This works also for pathnames with less '....' ;-)

But as someone mentioned already:
File::Basename might do it better.

T.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,159
Messages
2,570,879
Members
47,416
Latest member
LionelQ387

Latest Threads

Top