Very interesting, and potentially very, very useful. It might also be useful to have the reverse function, compacting a list of numbers into a list containing ranges.

The code below seems to do this, with the caveats that:

  1. if numbers appear twice in the list, only one instance is returned,
  2. the original ordering is lost, as the values in this code are sorted to ease processing, and
  3. the code is a sample, and someone more skilled would probably be able to do the same better or more efficiently

With that in mind, here is my contribution:

sub compactRanges { # # @pieces contains the list of numbers # %parts will contain a compact form, such that $start..$end # is represented as $parts{$start} = $end # $seperator contains the character(s) placed between the # values # %wrap_negatives contains a flag indicated if negative # values should be wrapped in some way, and the leading # and trailing values if so # my (@pieces) = @_; my $seperator = '..'; my %wrap_negatives = ( 'flag' => 1, 'leading' => '[', 'trailing' => ']' ); my (%parts); @pieces = sort { $a <=> $b } @pieces; { my ($i); my ($recent); $recent = $parts{$i} = $i = shift (@pieces); while (@pieces) { $i = shift (@pieces); $recent = $i if ( $i - 1 > $parts{$recent} ); $parts{$recent} = $i; } } foreach my $k ( sort { $a <=> $b } keys(%parts) ) { my $str = ( ( $wrap_negatives{'flag'} ) and ( $k < 0 ) ? $wrap_negatives{'leading'} : '' ) . $k . ( ( $wrap_negatives{'flag'} ) and ( $k < 0 ) ? $wrap_negatives{'trailing'} : '' ); $str .= $seperator . ( ( $wrap_negatives{'flag'} ) and ( $parts{$k} < 0 ) ? $wrap_negatives{'leading'} : '' ) . $parts{$k} . ( ( $wrap_negatives{'flag'} ) and ( $parts{$k} < 0 ) ? $wrap_negatives{'trailing'} : '' ) if ( $parts{$k} != $k ); push ( @pieces, $str ); } return (@pieces); }

In reply to Re: Expand Ranges in Lists of Numbers by atcroft
in thread Expand Ranges in Lists of Numbers by The Mad Hatter

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.