If you know the number of messages available ahead of time, you could pass that along with the range string to this function. It will return undef if the string is invalid, otherwise it will return a reference to an array containing each message number in question based on a given range.
sub range_populate {
my ($max, $range) = @_;
my @range = ();
if ($range eq 'a') { return \@{[0..$max]}; }
elsif ($range !~ /[-,]/) { push @range, $range; }
else {
my @mini = split /,/, $range;
for (@mini) {
return undef if /-.*?-/; # 1 dash per subsection, e.g., "2
+-6-9, 24" is invalid
if ( !/-/ ) { push @range, $_; }
else {
return undef unless /(\d+)-(\d+)/;
return undef unless $1 < $2;
push @range, ($1..$2);
}
}
}
return undef if $#range == -1;
for (@range) { return undef unless ($_ >= 0 && $_ <= $max); }
return \@range;
}
# example
my $num_messages = 10;
my $sample_range = '3-6,9';
my $rng_ref = &range_populate ($num_messages, $sample_range); # return
+s reference to array (3, 4, 5, 6, 9)
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.