Hi Monks, I know my title is vague, but I thought I'd let the actual content speak for itself. Here's what I want to do. I want to take this...

1,2,3,5,6,7,10,12,13,14,15

and turn it into this

1-3,5-7,10,12-15

I do have code and it works, however it just doesn't look perlish to me. I've always thought of regexes as basicly "can do anything with text". I tried a few and they just failed miserably. Here is my code...
#! /usr/bin/perl use strict; my $pages = "1,2,3,5,6,7,10,12,13,14,15"; my @pages_arr = split ',', $pages; my (@return_arr, $next_one, $lower_bound, $upper_bound); @return_arr = (); $next_one = ''; $lower_bound = ''; $upper_bound = ''; for (my $x = 0; $x < scalar(@pages_arr); $x++) { $next_one = $pages_arr[$x]; if ($x == 0) { $lower_bound = $upper_bound = $next_one; } elsif ($next_one == ($upper_bound + 1)) { $upper_bound = $next_one; if ($x == (scalar(@pages_arr) - 1)) { push @return_arr, "${lower_bound}-${upper_bound}"; } } elsif ($next_one != ($upper_bound + 1)) { if ($lower_bound == $upper_bound) { push @return_arr, $lower_bound; } else { push @return_arr, "${lower_bound}-${upper_bound}"; } $lower_bound = $upper_bound = $next_one; if ($x == (scalar(@pages_arr) - 1)) { push @return_arr, $next_one; + } } } my $return_var = join ',', @return_arr; print "return_var: ${return_var}\n";
It just looks so un-perlish. It does work and so far I haven't found a combination that breaks it. (The reason I say that is because this is part of a larger script that formats the input from the user like that. So, the numbers will always be in order and comma-delimited before it gets to that chunk of code.) However, it just looks like way too much code to do something so trivial. I won't ask if there is a better way, because Perl wouldn't be Perl if there wasn't. I'd just like to see what other Monks would do with this problem.

Thanks Monks.

In reply to Combine range of numbers. by the_0ne

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.