Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re^2: Splitting on a pattern once

by ungalnanban (Pilgrim)
on Mar 05, 2010 at 10:49 UTC ( #826940=note: print w/ replies, xml ) Need Help??


in reply to Re: Splitting on a pattern once
in thread Splitting on a pattern once


Is there any way to split the string from last occurrence.?
I tried with positive values and negative value in 3rd argument of split().
--sugumar--


Comment on Re^2: Splitting on a pattern once
Re^3: Splitting on a pattern once
by ikegami (Pope) on Mar 05, 2010 at 17:31 UTC
    You could play with reverse, but this is simpler:
    my ($k,$v) = /^(.*:)?(.*)/s;
Re^3: Splitting on a pattern once
by kiruthika.bkite (Scribe) on Mar 18, 2010 at 10:47 UTC
    Is this you are expecting.
    Example-1
    my $str="1 2 3 4 590"; my ($a,$b)=split(/ ([^ ]+)$/, $str); print "a=$a\nb=$b\n";

    Output
    a=1 2 3 4
    b=590

    Example-2
    my $str="1:2:3:4:590"; my ($a,$b)=split(/:([^:]+)$/, $str); print "a=$a\nb=$b\n";

    Output
    a=1:2:3:4
    b=590
Re^3: Splitting on a pattern once
by Anonymous Monk on Mar 18, 2010 at 11:04 UTC
    You should read the documentation for split as a negative limit has special meaning.
Re^3: Splitting on a pattern once
by kiruthika.bkite (Scribe) on Mar 22, 2010 at 10:58 UTC
    Use the following way to achieve your requirement.
    my $str="1:2:3"; my $name=(split(':',$str))[-1];
    -1 will return the value of last index.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (13)
As of 2015-07-06 20:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (83 votes), past polls