in reply to Regexp: Only tr second occurance

Here's a way using index and substr:

#!/usr/bin/perl my $str = "123.0456.0789.0-WHOOPS"; my $sub = ".0"; my $p = index($str,$sub,index($str,$sub)+1); print "str before = $str\n"; substr($str,$p,length($sub)) = "" unless $p < 0; print "str after = $str\n";

Or more generally,

#!/usr/bin/perl my $str = "123.0456.0789.0-WHOOPS"; my $sub = ".0"; print "str before = $str\n"; $str = rm_nth($str,$sub,2); print "str after = $str\n"; sub rm_nth { my ($str,$sub,$n) = @_; $n = 1 if $n < 1; my $p = -1; while ($n--) { $p = index($str,$sub,$p+1); return $str if $p < 0; } substr($str,$p,length($sub)) = ""; return $str; }

I'm sure someone will post the generalized regular expression version now :-)

Although, in Perl6 it will be so much nicer to say s:2nd/$pat//; or s:nth(2)/$pat//;

Update: Slight change to the assignment of $n in rm_nth so that it handles requests for deleting negative occurrences in a non-infinite-loop sort of manner :)