Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Split a line and only print up to a certain point 3

Status
Not open for further replies.

skw

Instructor
Jun 1, 1999
19
US
Can someone please help with how to get this done.
I have an array, let's call it
@directories ('my/dir/name','my/dir/games','my/dir/hobbies');

I have a search function setup, but the one problem I'm coming across, I have this statement

find(\&wanted, @directories);
if (scalar @filesfound) {
foreach $mystuff (@filesfound) {
print "\t$mystuff\n";

When I do a search, it prints out the entire dir path I have specified in @directories. What I want is to only print the last name in the path, e.g., it would not print
/my/dir/games but print games instead.

I know split suppose to take care of it but I don't know how to do it.

Thanks
 
$mystuff2=reverse $mystuff;
($dir_required, @crap)=split(/\//, $mystuff2);
$mystuff2=reverse $dir_required;

--Paul
 
Hi,

Try this:

foreach $mystuff (@filesfound) {
$mystuff=~/([^\/]+)$/;
print "\t$1\n";
}

Greetings

--
Smash your head on keyboard to continue...
 
or, if you wanted to do using split() like Paul, but forego all the reverse()'s,

$mystuff = (split("/",$mystuff))[-1];

--jim
 
Thank you all for the fast responses!

I picked and choose, and decided to use liuwt.
Codiferous example provides the same output as liuwt.

Thank you!

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top