Number::Range uses the ..
syntax that you have, but it doesn't have an iterator. Set::IntSpan uses -
instead of ..
, but it does have an iterator, which makes printing out the set without creating a giant array easy.
use Set::IntSpan;
my $str = "1..100,171,398..1000";
$str =~ s/\.\./-/g;
my $set = Set::IntSpan->new($str);
for (my $i = $set->first; defined $i; $i = $set->next) {
print "$i\n"; # Or however you'd like to format it
}
Internally, Set::IntSpan stores the ranges, so this should be fairly memory-efficient. You could also use the spans
method to get the parsed ranges. This would require you to write a bit more code, but would mean you don't have to do a method call for each number in the range. Method calls in Perl are a bit slow, but I wouldn't worry about it unless the above code takes too long to run.