@test = (23,23,4,8,21,90,90,90,90,2,2,2,19,21,19); map { $length = ($test[$_ - 1] == $last)? $length + 1: 1; $run++ unless $test[$_ - 1] == $last; $last = $test[$_ - 1]; $runlengths[$run] = [$test[$_ - 1], $length]; } (1 .. scalar @test); #### @strings = map { $runlengths[$_][0] . "x" . $runlengths[$_][1] } ( 1 .. $#runlengths); #### sub extract{ my $index = shift; my ($last, $lastindex); foreach (@runlengths[1 .. $#runlengths]){ ($last, $lastindex) = ($$_[0], $lastindex + $$_[1]); return $last if $index <= $lastindex - 1; } return undef; }