c:\@Work\Perl\monks>perl -wMstrict -le "my $s = join q{,}, 1 .. 40_000; ;; $s =~ s{ \b (\d+) (?{ $1 }) \K (?: , (\d+) \b (??{ ++$^R != $2 || length($1) != length($2) }))+ }{-$2}xmsg; print qq{'$s'}; " '1-9,10-99,100-999,1000-9999,10000-40000' c:\@Work\Perl\monks>perl -wMstrict -le "my $s = join q{,}, 1 .. 150_000; ;; $s =~ s{ \b (\d+) (?{ $1 }) \K (?: , (\d+) \b (??{ ++$^R != $2 || length($1) != length($2) }))+ }{-$2}xmsg; print qq{'$s'}; " Complex regular subexpression recursion limit (32766) exceeded at -e l +ine 1. Complex regular subexpression recursion limit (32766) exceeded at -e l +ine 1. Complex regular subexpression recursion limit (32766) exceeded at -e l +ine 1. '1-9,10-99,100-999,1000-9999,10000-42767,42768-75535,75536-99999,10000 +0-132767,132768-150000' c:\@Work\Perl\monks>perl -wMstrict -le "my $s = join q{,}, 1 .. 150_000; ;; $s =~ s{ \b (\d+) (?{ $1 }) \K (?: (?: , (\d+) \b (??{ ++$^R != $2 || length($1) != length($2) })){1 +,30000} )+ }{-$2}xmsg; print qq{'$s'}; " '1-9,10-99,100-999,1000-9999,10000-99999,100000-150000'

Updates:
  1. I should have mentioned that all these examples use the  \K regex operator, so they all require Perl version 5.10 or greater.
  2. Changed last two code examples (increased range to 150,000) to make contrast greater.
  3. (2016/10/03) If you can stomach the  \K operator (i.e., you're using Perl version 5.10+), here's a further tweak that speeds things up considerably. The critical enhancement is the use of the  "(?(condition)yes-pattern)" conditional expression (see Extended Patterns in perlre). Some other changes have been made, but they seem immaterial to the speed-up. Old (slower) version:
    c:\@Work\Perl\monks>perl -wMstrict -le "use 5.010; ;; print qq{perl version $]}; ;; my $s = join q{,}, 1 .. 1_500_000; ;; my $end_point = qr{ , (\d++) \b (??{ ++$^R != $2 || length($1) != length($2) }) }xms; ;; my $start = time; use re 'eval'; $s =~ s{ \b (\d++) (?{ $1 }) \K (?: $end_point{1,32766})+ }{-$2}xmsg; print qq{'$s'}; printf qq{in %d seconds \n}, time - $start; " perl version 5.010001 '1-9,10-99,100-999,1000-9999,10000-99999,100000-999999,1000000-1500000 +' in 18 seconds
    New (faster) version:
    c:\@Work\Perl\monks>perl -wMstrict -le "use 5.010; ;; print qq{perl version $]}; ;; my $s = join q{,}, 1 .. 1_500_000; ;; my $end_point = qr{ , (\d++) (?(?{ ++$^R != $2 || length($1) != length($2) }) (*F)) }xms; ;; my $start = time; use re 'eval'; $s =~ s{ \b (\d++) (?{ $1 }) \K (?: $end_point{1,32766})+ }{-$2}xmsg; print qq{'$s'}; printf qq{in %d seconds \n}, time - $start; " perl version 5.010001 '1-9,10-99,100-999,1000-9999,10000-99999,100000-999999,1000000-1500000 +' in 3 seconds

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


In reply to Re^9: Convert an array of numbers into a range by AnomalousMonk
in thread Convert an array of numbers into a range by rmocster

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.