Brother Monks,

A devilishly simple perl programming problem has hit me while trying to output some information to a web page in a condensed form. <as opposed to that amazingly long and uncondensed sentence>

I'm trying to condense a list of numbers e.g. (1 2 3 4 8 9) to a list of ranges (1-4,8,9). As I am reading and creating the data it can be stored in any form but I have chosen an array at this time so the program can walk through the list and it can be sorted if necessary. The actual lists of numbers may be quite a bit longer than the examples so this really would help in display space.

I did find the module Perlbug::Object::Range, but it comes with a LOT of baggage. It also doesn't handle singles nicely. It outputs (1-4,8-9) which is OK, but not what I would like to really see. My install of this module also seems to double up on single last numbers (1 2 3 4 8) becomes (1-4,8-8). I don't know if that is the intended action, but it is definitely not right.

Would any of the monks out there have a module or pre-written code to do this? I have started writing something, but I am still a perl novice and this works out to be a rather complex set of logic. I would rather use a pre-made wheel than carve my own so I can stick to the actual logic of the program and not worry so much about writing the formatting routines. I know it is a little lazy, but don't we all use standard CPAN modules so we can be just a little lazy.

-Kurt


In reply to condense list of numbers to ranges by KPeter0314

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.