Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Re: how to remove everything before last slash in perl?

by muba (Priest)
on Jun 23, 2012 at 20:08 UTC ( #978014=note: print w/replies, xml ) Need Help??

in reply to how to remove everything before last slash in perl?

Another approach, if you really want to use a regex, would be to capture just that part that you're interested in:

my $somestring = "y:/home/lib/directory/piano_book"; ($somestring) = $somestring =~ m#([^/]+)$#; print $somestring; __END__ piano_book

Update: or just to show that always TIMTOWTDI:

my $somestring = "y:/home/lib/directory/piano_book"; my $idx = rindex($somestring, "/"); $somestring = substr($somestring, $idx+1); print $somestring;

Replies are listed 'Best First'.
Re^2: how to remove everything before last slash in perl?
by Tux (Abbot) on Jun 23, 2012 at 22:58 UTC

    More than one way?

    my $filename = (split m{/} => $full_path)[-1];

    I agree however that File::Basename is way better, as it also deals with filesystems other than Unix-like that use a forward slash.

    Enjoy, Have FUN! H.Merijn

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://978014]
[choroba]: Yes, sometimes missing ; after the use clause just interprets the next line somehow and passes the result to the used module which could happily ignore it. Been there, seen that.
[Discipulus]: luckly the program was 20 lines!

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (9)
As of 2017-02-28 08:51 GMT
Find Nodes?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?

    Results (398 votes). Check out past polls.