http://www.perlmonks.org?node_id=1015544


in reply to Replacing values in an array

When dealing with duplicates in Perl, you should normally use a hash (perldoc -q duplicate). My solution (written before seeing toolic's) is essentially the same as his, though I excluded the check for "M" since all your test data contains "M".

use strict; use warnings; use Data::Dumper; my @array = ("M94202", "M94150", "M94297", "M94150", "M94161", "M94161 +", "M94162"); my %seen; my $z = 1; foreach my $item (@array) { if (exists $seen{$item}) { $item = $seen{$item}; } else { $seen{$item} = $z; $item = $z; $z += 2; } } print Dumper \@array; print "\n";