use strict; use warnings; use Data::Dumper; my @array = ("M94202", "M94150", "M94297", "M94150", "M94161", "M94161", "M94162"); my %uniq; my $z = 1; for (@array) { if (/M/) { if (exists $uniq{$_}) { $_ = $uniq{$_}; } else { $uniq{$_} = $z; $_ = $z; $z += 2; } } } print Dumper(\@array); __END__ $VAR1 = [ 1, 3, 5, 3, 7, 7, 9 ];