<[EMAIL PROTECTED]> wrote in message
news:[EMAIL PROTECTED]
> What is the function of cutting a string from a point until the last
character?
>
> For example
> $string="C:/progra~1/directory1/directory2/file.txt";
>
> i want to find the last backslash (/) of the string and keep the sequence
> following it (file.txt)
>
> Is it simple?

Yep

>
> I tried with the split function but it returns a list.
>
> I just want a scalar!
>

Well there are many ways you could do this. The 'best' way is probably to
use File::basename like this

 #!/perl -w
 use File::Basename;
 my $fullname = 'C:/progra~1/directory1/directory2/file.txt';
 $basename = basename($fullname);
 print "$basename\n";

...or if you fancy rolling your own you could do the faster...

 $last_slash_position = rindex($fullname, '/');
 my $basename2 = substr($fullname, $last_slash_position + 1);
 print "$basename2\n";

...or if you like regexes why not do this...

 my ($basename3) = ( $fullname =~ m/.*\/([^\/]*)$/);
 print "$basename3\n";

... but I'm getting carried away, go with file::basename and your code won't
break so often and you'll have a chance of it working on a different machine

Cheers

Rob

>



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to