use strict; use warnings; use Data::Dumper; my @a = ("M94202", "M94150", "M94297", "M94150", "M94161", "M94161", "M94162"); my ( $n, %h ) = -1; $_ = $h{$_} //= $n+=2 for @a; print Dumper \@a;