in reply to Expand Ranges in Lists of Numbers

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)

Replies are listed 'Best First'.
Re: (jeffa) Re: Expand Ranges in Lists of Numbers
by The Mad Hatter (Priest) on Jun 08, 2003 at 20:22 UTC
    Thanks, I didn't even think of doing it that way! :-) I would change
    $str =~ s/\s//g;
    to
    $str =~ s/[^\d-,]//g;
    though when handling untrusted data.

    As they say, TMTOWTDI.