kiruthika kiruthika - 5 months ago 31
Perl Question

How can I split a Perl string only on the last occurrence of the separator?

my $str="1:2:3:4:5";
my ($a,$b)=split(':',$str,2);


In the above code I have used limit as 2 ,so $a will contain 1 and remaining elements will be in $b.
Like this I want the last element should be in one variable and the elements prior to the last element should be in another variable.

Example

$str = "1:2:3:4:5" ;
# $a should have "1:2:3:4" and $b should have "5"
$str = "2:3:4:5:3:2:5:5:3:2"
# $a should have "2:3:4:5:3:2:5:5:3" and $b should have "2"

YOU YOU
Answer
split(/:([^:]+)$/, $str)