in reply to Convert an array of numbers into a range

<pedantry>

... an array with padded numbers ... (0001,0002,0003,011,012,013,015) ...

Just BTW, and perhaps to avoid a subsequent misunderstanding, that's an array of octal numbers.

c:\@Work\Perl\monks>perl -wMstrict -le "use Data::Dump qw(dd); ;; my @ra = (0001,0002,0003,011,012,013,015); dd \@ra; " [1, 2, 3, 9, 10, 11, 13]
See in particular the "true" (i.e., decimal) value of 015.

Might you be thinking of something like  qw(0001 0002 0003 011 012 013 015) instead?

c:\@Work\Perl\monks>perl -wMstrict -le "use Data::Dump qw(dd); ;; my @ra = qw(0001 0002 0003 011 012 013 015); dd \@ra; " ["0001", "0002", "0003", "011", "012", "013", "015"]

</pedantry>


Give a man a fish:  <%-{-{-{-<

Replies are listed 'Best First'.
Re^2: Convert an array of numbers into a range
by rmocster (Novice) on Jul 22, 2016 at 00:26 UTC
    I am thinking @ra = qw(0001 0002 0003 011 012 013 015); or added couple numbers :) @ra = qw(0001 0002 0003 011 012 013 015 16 17 18 20); How is regex be structured? Thanks a lot.

      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;

        this sort of works.

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

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

      #!/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); print join(',', @ra) =~ s/\b(\d+)(?{$1})\K(?:,(\d+)\b(??{++$^R!=$2}))+ +/-$2/gr;