my $lo_stretch = 0; my $max_lo_stretch = 0; my $max_lo_stretch_end; my $hi_stretch = 0; my $max_hi_stretch = 0; my $max_hi_stretch_end; for my $i ( 0..@avg ) { if ( $i <= $#avg && $high[$i] > $avg[$i] ) { ++$hi_stretch; } else { if ( $hi_stretch > $max_hi_stretch ) { $max_hi_stretch = $hi_stretch; $max_hi_stretch_end = $i - 1; } $hi_stretch = 0; } # repeat above for lo, only with $high[$i] < $avg[$i] }