use warnings; use strict; my $counter = 0; my %hash; while () { my @splits = split; my $snp = $splits[0]; $hash{$snp} ||= ++$counter; print "$hash{$snp} $snp\n"; } __DATA__ foo abc foo cde bar xyz foo hij #### 1 foo 1 foo 2 bar 1 foo #### 1 foo 1 foo 2 bar 2 foo