You may use existing solutions, like Bit::Vector:
use Bit::Vector;
my @probs =(0,0.1,0.53,0.51,0.59,0.67,0.2,0.04,0.05,0.56,0.89,0.75);
my $v = Bit::Vector->new(int @probs);
$v->Index_List_Store(grep { $probs[$_] > .5 } 0..$#probs);
for (my ($start, $min, $max) = 0; $start < $v->Size(); $start = $max +
+ 2) {
last unless ($min, $max) = $v->Interval_Scan_inc($start);
print "$min $max\n";
}
or even use $v->to_Enum instead of above for loop and receive
2-5,9-11
As a marginal note, you use indexes out of bounds of the array (in $probs[$_+1], when $_=$#probs), which is an error-prone practice.
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.