in reply to How to cut strings from the end

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.

Replies are listed 'Best First'.
Re^2: How to cut strings from the end
by izut (Chaplain) on May 30, 2006 at 13:25 UTC

    Could you explain how this works? I was trying these days to dismiss the first element of an anonymous list, then I got this code:

    (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.

      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.

      Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
      How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart