#!/usr/bin/perl use warnings; use strict; while(my $c = <>){ $_ = <>; s/ .*?(\b\d+)[ ] (?=(\d+)\b) (??{ print "[$1 $2]\n"; $2 - $1 <= $c ? 'X' : '' }) / (print "DELETED:[$&] REMAINS:[$']\n"), '' /egx; printf "NUMBERS:[%d:%s]\n", (length s/\d//gr), $_; print '-' x 10, "\n"; } #### 1 1000000000 5 1 1 1 2 2 2 1 1 3 5 7 9 10 1 1 2 3 4 5 28 31 36 43 5 1 3 8 14 19 26 33 1 1 2 3 4 #### NUMBERS:[1:1000000000 ] ---------- [1 1] [1 1] [1 2] [2 2] [2 2] [1 1] [1 2] [2 2] [2 2] [1 1] [1 2] [2 2] [2 2] [1 2] [2 2] [2 2] [1 2] [2 2] [2 2] [2 2] [2 2] [2 2] [2 2] [2 2] [2 2] NUMBERS:[6:1 1 1 2 2 2 ] ---------- [1 3] DELETED:[1 ] REMAINS:[3 5 7 9 10 ] [3 5] DELETED:[3 ] REMAINS:[5 7 9 10 ] [5 7] DELETED:[5 ] REMAINS:[7 9 10 ] [7 9] DELETED:[7 ] REMAINS:[9 10 ] [9 10] NUMBERS:[2:9 10 ] ---------- [1 2] [2 3] [3 4] [2 3] [3 4] [2 3] [3 4] [3 4] [3 4] NUMBERS:[4:1 2 3 4 ] ---------- [28 31] [31 36] [36 43] DELETED:[28 31 36 ] REMAINS:[43 ] NUMBERS:[1:43 ] ---------- [1 3] [3 8] [8 14] DELETED:[1 3 8 ] REMAINS:[14 19 26 33 ] [14 19] [19 26] DELETED:[14 19 ] REMAINS:[26 33 ] Can't coerce UNKNOWN to string in substitution iterator at ./coerce.pl line 11, <> line 12.