in reply to Re: Re: backward split
in thread backward split

Sorry, misinterpreted your intent. This should work to split it that way then:
#!/usr/bin/perl -w use strict; my $string = "foo.bar.foobar"; my ($val1,$val2) = split /\.(?!.*\.)/,$string; print "$val1,$val2\n";

update:and another way:

#!/usr/bin/perl -w use strict; my $string = "foo.bar.foobar"; my $position = rindex($string,'.'); my ($val1,$val2) = ( substr($string,0,$position++), substr($string,$position) ); print "$val1,$val2\n";
update:Apparently, I suffer from not enough laziness, as in the end would probably have kept thinking there has to be a more elegant solution, and would hopefully stumbled upon sauoq's solution below at some point, which I think is the best solution, but leave the above code if only to show TMTOWTDI.

-enlil

Replies are listed 'Best First'.
Re: Re: Re: Re: backward split
by sauoq (Abbot) on Jun 01, 2003 at 01:37 UTC

    Of course, you can use a positive lookahead too...

    my ($val1, $val2) = split /\.(?=[^.]*$)/, $string;
    but really... neither choice is better than a simple capturing regex.

    -sauoq
    "My two cents aren't worth a dime.";