@array=qw(x x y z z z); $c=$array[0]; $start=0; $end=0; for $index (1..$#array){ if (!($c eq $array[$index])){ push(@output, $start, $end); $c=$array[$index]; $start=$index; $end=$index; }else{ $end=$index; } } push(@output, $start, $end); print join(" ", @output), "\n"; Outputs: 0 1 2 2 3 5