From 2d6c93c03ba8a2392ff09b92bfd51fb8afe7a23c Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Thu, 19 Aug 2021 20:58:01 +0200 Subject: incdec: Get backward incrementing working in middle of a number --- incdec.pm | 19 ++++++++++++------- t/100-increment-decrement.t | 6 ++++++ 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/incdec.pm b/incdec.pm index 9569171..b05c547 100644 --- a/incdec.pm +++ b/incdec.pm @@ -12,17 +12,22 @@ sub incdec { my $start_position = 0; my @points = (); my $previous_match_start = 0; + my $previous_match_end = 0; while ($line =~ /(\d+)/g) { if ($is_backward) { - $previous_match_start = $-[0]; - + # print "pos[$point_position] -[$-[0]]- +[$+[0]]+\n"; # if $point_position < $-[0] # && previous $+[0] <= $point_position - # if ($point_position >= $-[0]) { - # $start_position = $-[0]; - # - # last; - # } + # if ($previous_match_end <= $point_position && $point_position < $-[0]) { + if ($-[0] <= $point_position && $point_position < $+[0]) { + # print "matched"; + $start_position = $-[0]; + + last; + } + + $previous_match_start = $-[0]; + $previous_match_end = $+[0]; } else { if ($point_position < $+[0]) { diff --git a/t/100-increment-decrement.t b/t/100-increment-decrement.t index b234f83..1345dfe 100644 --- a/t/100-increment-decrement.t +++ b/t/100-increment-decrement.t @@ -61,4 +61,10 @@ is( 'increments the fourth double-digit integer with point at position 16 backward' ); +is( + incdec::incdec('test 12 19 555 64', 13, 1), + 'test 12 19 556 64', + 'increments the third triple-digit integer with point at position 13 backward' +); + done_testing; -- cgit v1.2.3