Last character in a string?


I haven’t programmed in Perl in a long time, so I have forgotten many things. How do I get the last character in a string? So if $username = “testing”, how to I get the last characters of $username?

you can use substr() or a regexp or split() but split is the least desireable unless you had other processing to do that required splitting the string into individual characters.

I was hoping on something much easier like $username[-1] or something.

Unless you have totally forgotten perl you should know that is for returning the last element of an array. Use substr():

$foo = ‘fox’;
$last = substr $foo,-1,1;