If [your original code] had more error-checking [and features like it does now], i wouldn't say that you are doing too much work. Using eval here gives pretty much the exact same results and requires hardly any effort on the coder's behalf ... just get rid of spaces and change all dashes to two dots:
use Data::Dumper; print Dumper [evalexp('21, 35, 42-45, 19')]; sub evalexp { my $str = shift; $str =~ s/\s//g; $str =~ s/-/../g; return eval $str; }
By the way, in your example, this line:
push @expanded, $piece;
pushes "strings" onto the array. You probably should "cast" $piece with int:
push @expanded, int $piece;
instead. (The ranges don't suffer from this side effect [caused by split i believe] because of the .. operator.) Data::Dumper reveals such details.

Happy coding. :)

jeffa

L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
B--B--B--B--B--B--B--B--
H---H---H---H---H---H---
(the triplet paradiddle with high-hat)

In reply to (jeffa) Re: Expand Ranges in Lists of Numbers by jeffa
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.