Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
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--

Replies are listed 'Best First'.
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 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.
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 Anonymous Monk on Mar 18, 2010 at 11:04 UTC
    You should read the documentation for split as a negative limit has special meaning.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://826940]
help
Chatterbox?
[stevieb]: ...or the proper download link and release page even...
[pryrt]: da39a3ee5e6b4b0d32 55bfef95601890afd8 0709 *strawberry-perl-5 .12.3.0-portable. zip
[pryrt]: matches neither. :-)
[stevieb]: wtf!? lol
[stevieb]: I'm checking some other versions. This is a little frightening
[pryrt]: Sorry, my bad: it was't done downloading when I did the sha1sum... now I match your 309ad... value

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (7)
As of 2017-03-29 21:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Should Pluto Get Its Planethood Back?



    Results (353 votes). Check out past polls.