Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

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

by 7stud (Deacon)
on Jun 23, 2012 at 21:32 UTC ( #978016=note: print w/ replies, xml ) Need Help??


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

Your regex:

$output =~ s{.*(/)}{$1}

...says to match any number of characters followed by a slash, then replace the whole match with a slash. As a result, if there is a match there is going to be a slash in the output. There's actually no point in even specifying $1 because your substitution is the same as:

s{.*/}{/}

Here are some other solutions:

1.

use strict; use warnings; use 5.010; use File::Basename; my @paths = ( "y:/home/lib/directory/book", "y:/home/lib/directory/book_manager", "y:/home/lib/directory/piano_book", ); my @results = map basename($_), @paths; say "@results"; --output:-- book book_manager piano_book

2.

@results = map { (split '/', $_)[-1] } @paths;


Comment on Re: how to remove everything before last slash in perl?
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://978016]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (12)
As of 2014-07-28 16:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (204 votes), past polls