in reply to last character in string ?

Hmmm, any of the below should work :
chomp($date); # remove newlines chop($date); # remove last char $date =~ s/\s*$//; # remove trailing spaces $date = substr($date,0,(length($date)-1)); # remove last char ($date) = $date =~ m/(.*)\s*$/; # i would imagine the slowest solution

"They shall not overcome. Whoever told them that the truth shall set them free was obviously and grossly unfamiliar with federal law."
    -- John Ashcroft

Replies are listed 'Best First'.
Perl that moves.
by boo_radley (Parson) on Oct 01, 2001 at 17:15 UTC
    you suggested :

    $date = substr($date,0,(length($date)-1)); # remove last char

    It's worth noting that perl uses negative numbers to "count backwards" when dealing with string (and array, and probably many other surprising things) indexes, removing the need to do things like length($date)-1)). Code looks nicer and scans better for the effort.
    See Piercer's code below.

    Update : Wouldn't it be nice if push, pop and (un)shift worked on characters in a string? Yes, there's chop. that's true enough, but still.

      In regard to your update, you should check out Tie::CharArray

      :-)

      Yves
      --
      You are not ready to use symrefs unless you already know why they are bad. -- tadmc (CLPM)

      @l=split(//,$date);pop(@l);foreach(@l){$r.=$_}print$r;

      What fun :)

      --
      my @words = grep({ref} @clever);
      sub version { print "I cuss your capitalist pig tendencies bad!\n" }
        Hi Amoe

        Just thought that someone should mention that your code could be written as so:

        @l=split//,$date;pop @l;print join"",@l;
        The two points I want to make are first, if you are trying to make your code as small as possible then often the parentheses can be left out (as well as trailing semicolons in a scope). Second that the foreach(@l){$r.=$_} is better written as $r=join"",@l;.

        Anyway,

        :-)

        Yves
        --
        You are not ready to use symrefs unless you already know why they are bad. -- tadmc (CLPM)