Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: How to cut strings from the end

by b10m (Vicar)
on May 30, 2006 at 11:08 UTC ( [id://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]);
--
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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (6)
As of 2024-03-28 15:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found