in reply to Re^5: Convert an array of numbers into a range
in thread Convert an array of numbers into a range

WOW, that is perfect. Big Thanks. Can you give a brief explanation of the syntax if you don't mind? Again thanks.
  • Comment on Re^6: Convert an array of numbers into a range

Replies are listed 'Best First'.
Re^7: Convert an array of numbers into a range
by Anonymous Monk on Jul 22, 2016 at 01:43 UTC
    #!/usr/bin/perl -l # http://perlmonks.org/?node_id=1168288 use strict; use warnings; my @ra = qw(0001 0002 0003 011 012 013 015 16 17 18 20); $_ = join ',', @ra; s/\b # find a number boundary (\d+) # and a number $1 (?{$1}) # save the number in $^R \K # then leave the above in the string (?: # non-capture grouping , # comma - also forces $1 to be a complete number (\d+) # a complete number (because of , and \b) \b # forces comlete number (??{ # extended pattern ++$^R != $2 # false if correct number || length$1 != length$2 # false if same length }) # false (and therefor matches) if both false, other +wise fails # if true returns 1 which can't match because of th +e \b )+ # repeat at least once /-$2/gx; # replace second or more with last matching number print;