Beefy Boxes and Bandwidth Generously Provided by pair Networks vroom
There's more than one way to do things
 
PerlMonks  

Re: split string and always get last word

by GrandFather (Saint)
on Mar 01, 2006 at 21:11 UTC ( [id://533788]=note: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.


in reply to split string and always get last word

Note that the "magic" in the two answers that you have been given already is the [-1]. Index -1 gets you the last element in an array. -2 gets the penultimate element and so on.

Also of interest is $#array which gives the index number of the last element in @array. That is (generally) different than scalar @array which returns the number of elements in @array.


DWIM is Perl's answer to Gödel

Replies are listed 'Best First'.
Re^2: split string and always get last word
by sulfericacid (Deacon) on Mar 01, 2006 at 21:22 UTC
    I'll post a non-split solution. IRL I'd probably use a regex for this anyway.
    my $string = "/alex/samsung/t2"; $string =~ m/(.+)\/(.+)$/; print "$1, $2";


    "Age is nothing more than an inaccurate number bestowed upon us at birth as just another means for others to judge and classify us"

    sulfericacid
      Another regexp if you don't need the path:
      my $string = "/alex/samsung/t2"; $string =~ m/([^\/]+)$/; print $1, "\n";
Re^2: split string and always get last word
by linux454 (Pilgrim) on Mar 01, 2006 at 21:53 UTC
    Give a man a fish, feed him for a day. Teach a man to fish, feed him for a lifetime. Thanks for providing the detail.

      Give a man a fish, feed him for a day and fuel his appetite for your fish. Teach a man to fish and you lose your monopoly. :)


      DWIM is Perl's answer to Gödel
        I have never understood this 'Give a man a fish' line. It's often used in the context of food aid given by the western world to the starving poor in some distant country. Generally speaking, the native peoples of the world are quite good at extracting fish from rivers, lakes and the sea. I can only assume that they would fish for themselves unless there was a good reason why not.

        Interestingly, there is a growing awareness that we should be looking back at the agricultural methods of our ancestors and the native peoples in order to feed ourselves in the 21st century without completely trashing the environment.

        Give a man a fish and he will point out that the sea is full of fish it's just that all of the boats were destroyed in the tsunami. Give a group of men a boat ...

Re^2: split string and always get last word
by AnomalousMonk (Archbishop) on Aug 14, 2021 at 12:42 UTC
    ... $#array ... gives the index number of the last element in @array. That is (generally) different than scalar @array which returns the number of elements in @array.

    TWIMC: Of historical interest: The single circumstance in which $#array will not be different than scalar @array (i.e., @array evaluated in scalar context) is when the array base $[ is set to 1. This is no longer possible, and wasn't a good idea when it was possible.

    See $[ in Deprecated and removed variables in perlvar for gory details.


    Give a man a fish:  <%-{-{-{-<

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://533788]
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.