Re: split string and always get last word
by linux454 (Pilgrim) on Mar 02, 2006 at 01:49 UTC
|
my $string = "./t2";
my $string2 = "/some/path/to/t3";
my $base = (split(/\//, $string))[-1];
my $base2 = (split(/\//, $string2))[-1];
print "$base\n$base2\n";
| [reply] [d/l] |
|
my ($string, $string2) = ("./t2", "/some/path/to/t3");
print join "\n", map {(split /\//)[-1]} ($string, $string2);
Prints:
t2
t3
This is about "get last word" isn't it :)
DWIM is Perl's answer to Gödel
| [reply] [d/l] [select] |
Re: split string and always get last word
by diotalevi (Canon) on Mar 02, 2006 at 01:49 UTC
|
Use a list slice. my $last = ( split '/', $str )[ -1 ];
| [reply] [d/l] |
Re: split string and always get last word
by GrandFather (Saint) on Mar 02, 2006 at 02:11 UTC
|
Note that the "magic" in the two answers that you have been given already is the [-1]. Index -1 gets you the last element in an array. -2 gets the penultimate element and so on.
Also of interest is $#array which gives the index number of the last element in @array. That is (generally) different than scalar @array which returns the number of elements in @array.
DWIM is Perl's answer to Gödel
| [reply] [d/l] [select] |
|
... $#array ... gives the index number of the last element in @array. That is (generally) different than scalar @array which returns the number of elements in @array.
TWIMC: Of historical interest: The single circumstance in which $#array will not be different than
scalar @array (i.e., @array evaluated in scalar context) is when the array base $[ is set to 1. This is no longer possible, and wasn't a good idea when it was possible.
See $[
in
Deprecated and removed variables
in perlvar for gory details.
Give a man a fish: <%-{-{-{-<
| [reply] [d/l] [select] |
|
I'll post a non-split solution. IRL I'd probably use a regex for this anyway.
my $string = "/alex/samsung/t2";
$string =~ m/(.+)\/(.+)$/;
print "$1, $2";
"Age is nothing more than an inaccurate number bestowed upon us at birth as just another means for others to judge and classify us"
sulfericacid
| [reply] [d/l] |
|
Another regexp if you don't need the path:
my $string = "/alex/samsung/t2";
$string =~ m/([^\/]+)$/;
print $1, "\n";
| [reply] [d/l] |
|
Give a man a fish, feed him for a day.
Teach a man to fish, feed him for a lifetime.
Thanks for providing the detail.
| [reply] |
|
| [reply] |
|
Re: split string and always get last word
by blokhead (Monsignor) on Mar 02, 2006 at 03:01 UTC
|
Looks like you're trying to get the filename component of a file path. For this particular task, a more portable solution is to use the basename function from File::Basename, which is comes with the standard Perl distribution.
| [reply] |
Re: split string and always get last word
by revdiablo (Prior) on Mar 02, 2006 at 03:06 UTC
|
Your strings look like paths. If this is the case, you can make your code nicely portable by using File::Spec. Example:
use File::Spec;
my $path1 = "./t2";
my $path2 = "/alex/samsung/t2";
my $end1 = ( File::Spec->splitdir($path1) )[-1];
my $end2 = ( File::Spec->splitdir($path2) )[-1];
print "Found $end1 at the end of $path1\n";
print "Found $end2 at the end of $path2\n";
| [reply] [d/l] |
Re: split string and always get last word
by Roy Johnson (Monsignor) on Mar 02, 2006 at 15:17 UTC
|
my $str = '/alex/samsung/t2';
my $last_element = substr($str, rindex($str, '/') + 1);
print "$last_element\n";
Note that if the delimiter is not present, you'll get the whole string.
Caution: Contents may have been coded under pressure.
| [reply] [d/l] |
Re: split string and always get last word
by Praveen (Friar) on Mar 02, 2006 at 05:46 UTC
|
while(<DATA>)
{
print "$'", if($_ =~ /(.*)\//g);
}
__DATA__
./t2
/some/path/to/t3
/some/t2
| [reply] [d/l] |