# now we'll pop away what we found from the AoH # basically we splice out subsequent indexes in one go # and the poor lonelies have to go home alone # if nothing is to be popped, we don't pop unless (scalar @i_to_delete) { return $text; } @i_to_delete = sort {$b <=> $a} @i_to_delete; # descending order my $length = 1; my $offset = undef; for my $i (0 .. $#i_to_delete) { if ($i < $#i_to_delete) { # not the last index if (($i_to_delete[$i] - $i_to_delete[$i+1]) == 1) { # the two are subsequent $length++; } else { # they are not subsequent, split current $offset = $i_to_delete[$i]; splice(@$AoH, $offset, $length); $length = 1; } } else { # last index $offset = $i_to_delete[$i]; splice(@$AoH, $offset, $length); $length = 1; } }