Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re^3: Convert an array of numbers into a range

by Anonymous Monk
on Jul 22, 2016 at 00:49 UTC ( [id://1168296]=note: print w/replies, xml ) Need Help??


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;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1168296]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (2)
As of 2024-04-26 02:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found