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

For older perls without the /r

#!/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(\d+)(?{$1})\K(?:,(\d+)\b(??{++$^R!=$2}))+/-$2/g; print;

Replies are listed 'Best First'.
Re^4: Convert an array of numbers into a range
by rmocster (Novice) on Jul 22, 2016 at 00:57 UTC

    this sort of works.

    result: 0001-0003,011-013,015-18,20

    should be: 0001-0003,011-013,015,16-18,20

      Changing the rules in the middle of the game?

      Next time give exact desired output when you pose a problem.

        Sorry, not meant to mislead.
      #!/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(\d+)(?{$1})\K(?:,(\d+)\b(??{++$^R!=$2||length$1!=length$2}))+/-$2 +/g; print;
        WOW, that is perfect. Big Thanks. Can you give a brief explanation of the syntax if you don't mind? Again thanks.

        I got this error.

        Complex regular subexpression recursion limit (32766) exceeded

        s/\b(\d+)(?{$1})\K(?:,(\d+)\b(??{++$^R!=$2||length$1!=length$2}))+/-$2 +/g;