From 1d27b8f1769c010fa097bee11cb9236b243e720c Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Sun, 22 Aug 2021 23:03:19 +0200 Subject: Add incdec.pl This was from when I extracted the `incdec` function to a standalone Perl script to test what was wrong with negative numbers in the second position. --- incdec.pl | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100755 incdec.pl (limited to 'incdec.pl') diff --git a/incdec.pl b/incdec.pl new file mode 100755 index 0000000..e60922e --- /dev/null +++ b/incdec.pl @@ -0,0 +1,56 @@ +#!/usr/bin/env perl -s +# +use strict; +use warnings; + +sub incdec { + my ($line, $increment_by, $point_position, $is_backward) = @_; + + $point_position ||= 0; + $is_backward ||= 0; + + my $start_position = 0; + my $previous_match_start = 0; + while ($line =~ /\b(-?\d+)\b/g) { + if ($is_backward) { + if ($point_position < $-[0]) { + $start_position = $previous_match_start; + + last; + } + + $previous_match_start = $-[0]; + } + else { + if ($point_position < $+[0]) { + $start_position = $-[0]; + + last; + } + } + } + + if ($is_backward && $point_position == length $line) { + $start_position = $previous_match_start; + } + + pos($line) = $start_position; + $line =~ s/\G([^-\d]*)(-?\d+)/$1 . ($2 + $increment_by)/e; + + return $line; +} + +# my $line = 'test 1 0'; +# my ${"increment-by"} = -1; +# my ${"point-position"} = 8; +# my $backward = 1; + +my $line = 'test 1 -2'; +my $increment_by = -1; +my $point_position = 9; +my $backward = 1; + +print "$line, $increment_by, $point_position, $backward\n"; +my $output = incdec($line, $increment_by, $point_position, $backward); +print $output; +print "\n"; -- cgit v1.2.3