for my $i ($#history .. 0 ) { !exists $history{ history[$i] } # use exists as testing its value against zero will # autovivify (which is why we need to set its value to 1) ? $history{ $history[$i] }++ # autovivify and increment. (By using exists, the # setting to 1 is redundant but... : splice @history, $i--, 1); # the -- in this version would be bad!! # Thou shalt not modify the iterator in a for loop! }