Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

Re: How to cut strings from the end

by b10m (Vicar)
on May 30, 2006 at 11:08 UTC ( #552465=note: print w/replies, xml ) Need Help??

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]);

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

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://552465]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (1)
As of 2022-12-06 20:06 GMT
Find Nodes?
    Voting Booth?

    No recent polls found