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); #### 2 5 9 11 #### my $positions = undef; my @segment=(); foreach (0..$#probs){ if ($probs[$_]>0.5 && $probs[$_+1]>0.5){ $positions = $positions.','.$_; } else {push @segment, $positions if ($positions); $positions = undef} }