in reply to How do I capture the last character in a string?

I'm feeling creative...
#!/usr/bin/perl -w use strict; my $string = "This is a string"; my $char = (unpack("a", reverse $string))[0]; print $char;

No, not faster, or shorter, just Another Way :-P

Arjen

Replies are listed 'Best First'.
Re^2: How do I capture the last character in a string?
by Aristotle (Chancellor) on Apr 30, 2003 at 14:53 UTC
    my ($char) = unpack sprintf("x%d A", length($string)-1), $string;

    Makeshifts last the longest.

Re: Re: How do I capture the last character in a string?
by TVSET (Chaplain) on Apr 30, 2003 at 17:09 UTC
    Yet another way:

    #!/usr/bin/perl -w use strict; my $str = "hello, world!"; my $result = substr($str,length($str)-1,1); print $result;

    PS: Surprisingly, length($str)-- doesn't work for me, but length($str)-1 does.

    Leonid Mamtchenkov aka TVSET