in reply to printing array positions that match a condition
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.
|
|---|