Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
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 drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2014-11-28 03:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (192 votes), past polls