Welcome to the Monastery 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.

Create A New User
Node Status?
node history
Node Type: note [id://826940]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (11)
As of 2018-05-24 12:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
World peace can best be achieved by:

Results (177 votes). Check out past polls.

Notices?