my $string = 'perl345';
my $tag = substr $string, -3;
Or if you want to remove the last three characters, too,
my $string = 'monks 1223';
my $tag = substr $string, -3, 3, '';
| [reply] [d/l] [select] |
Hi Bharath,
You have to take a look at substr or you can use regex to accomplish your need. Please do Super Search before posting.
$string =~ s/.{3}$//;
| [reply] [d/l] |
You can use substr() to extract a substring from a string. You'll probably want to use the two-argument version (see the docs).
| [reply] |
Of course the "right answer" is already given, yet if you want to go crazy, you might also abuse split for this ;-)
my $string = 'monk1223';
print ((split "", $string)[$#a-2..$#a]);
--
b10m
All code is usually tested, but rarely trusted.
| [reply] [d/l] |
(undef, @list) = split /\s+/, $str;
I was wondering if something like that could work, but it doesn't:
@list = (split /\s+/, $str)[1..$#a];
Thanks in advance.
Igor 'izut' Sutton
your code, your rules.
| [reply] [d/l] [select] |
His code assumes @a is empty, which means $#a returns -1, so he's doing (...)[-1 - 2 .. -1] which is just (...)[-3 .. -1]. Your case cannot be handled in a similar manner -- you'd need to know the size of the list being returned by split(). Your first approach is fine.
| [reply] [d/l] [select] |
The matter has been covered recently: substring problem???. Only, in that occasion the OP needed the last four characters in the string ;)
Flavio
perl -ple'$_=reverse' <<<ti.xittelop@oivalf
Don't fool yourself.
| [reply] |
$string = 'perl345';
$_ = $string;
chop;
chop;
chop; #!
$string = $_;
Unless I have the sense of your question reversed, in which case this will work:
$string = "perl345";
for( reverse split //, $string ){
$newstring .= chop unless /[a-z]/
}
print scalar reverse $newstring;
;-)
--chargrill
$,=42;for(34,0,-3,9,-11,11,-17,7,-5){$*.=pack'c'=>$,+=$_}for(reverse s
+plit//=>$*
){$%++?$ %%2?push@C,$_,$":push@c,$_,$":(push@C,$_,$")&&push@c,$"}$C[$#
+C]=$/;($#C
>$#c)?($ c=\@C)&&($ C=\@c):($ c=\@c)&&($C=\@C);$%=$|;for(@$c){print$_^
+$$C[$%++]}
| [reply] [d/l] [select] |