Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I need to capture the last character in a string.

I know that I can split the entire string up into an array, and grab the last elemnt, but is there an easier more efficient way?

Thanks

  • Comment on How do I capture the last character in a string?

Replies are listed 'Best First'.
Re: How do I capture the last character in a string?
by broquaint (Abbot) on Apr 30, 2003 at 13:52 UTC
    Why substr() of course
    my $str = "a string"; print substr($str, -1); __output__ g
    You could also use regex, but I imagine (see. hope) this would be more efficient.
    HTH

    _________
    broquaint

Re: How do I capture the last character in a string?
by Corion (Patriarch) on Apr 30, 2003 at 13:54 UTC

    There are many ways to get the last character from a string :

    use strict; my $string = "Hello World"; my $char; # Use a capturing parenthesis $char = $1 if ($string =~ /(.)\Z/); print ">>$string<<\n"; print "[[$char]]"; # Chop the last char away from $string # this modifies $string $char = chop $string; print ">>$string<<\n"; print "[[$char]]"; # use substr : $char = substr($string,-1); print ">>$string<<\n"; print "[[$char]]";

    perl -MHTTP::Daemon -MHTTP::Response -MLWP::Simple -e ' ; # The $d = new HTTP::Daemon and fork and getprint $d->url and exit;#spider ($c = $d->accept())->get_request(); $c->send_response( new #in the HTTP::Response(200,$_,$_,qq(Just another Perl hacker\n))); ' # web
Re: How do I capture the last character in a string?
by dragonchild (Archbishop) on Apr 30, 2003 at 13:54 UTC
    my ($last) = $x =~ /(.)$/;
    This'll work in nearly all applications.
    my $last = substr $x, -1, 1;
    That'll work in all applications.

    ------
    We are the carpenters and bricklayers of the Information Age.

    Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

    Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

Re: How do I capture the last character in a string?
by perlplexer (Hermit) on Apr 30, 2003 at 13:55 UTC
    1. $last = substr $foo, -1, 1;
    2. ($last) = $foo =~ /(.?)$/;

    --perlplexer
Re: How do I capture the last character in a string?
by Aragorn (Curate) on Apr 30, 2003 at 14:17 UTC
    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

      my ($char) = unpack sprintf("x%d A", length($string)-1), $string;

      Makeshifts last the longest.

      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

Re: How do I capture the last character in a string?
by Anonymous Monk on Apr 30, 2003 at 13:56 UTC
    Thanks guys

    I always forget about 'chop'

    Thanks again!

      Note that chop() alters the string it's operating on.

      --perlplexer
      Just for the sake of completeness, I wanted to make sure you didn't also forget about 'chomp' - here's the 1st few lines of 'perldoc -f chomp':
      chomp VARIABLE chomp( LIST ) chomp This safer version of "chop" removes any trailing strin +g that corresponds to the current value of $/ (also known as $INPUT_RECORD_SEPARATOR in the "English" module). It r +eturns the total number of characters removed from all its arg +uments. It’s often used to remove the newline from the end of a +n input record when you’re worried that the final record may be + missing its newline. When in paragraph mode ("$/ = """), it re +moves all trailing newlines from the string. When in slurp m +ode ("$/ = undef") or fixed-length record mode ($/ is a referenc +e to an integer or the like, see perlvar) chomp() won’t remove +any- thing. If VARIABLE is omitted, it chomps $_. Example: while (<>) { chomp; # avoid \n on last field @array = split(/:/); # ... }
      HTH.