use strict; use warnings; use Data::Dumper; my (@array, $iIndex, $thisValue, $lastValue, @out); @array = qw(x x y z z z); @out = (0); $lastValue = $array[0]; for ($iIndex = 0; $iIndex <= $#array; $iIndex++) { $thisValue = $array[$iIndex]; if ($thisValue ne $lastValue) { push @out, ($iIndex - 1, $iIndex); $lastValue = $thisValue; } } push @out, $#array; print Dumper(\@out);