in reply to only increment counter if the ID has not been seen before

You need to populate the hash inside the loop somehow. Here is one way:
use warnings; use strict; my $counter = 0; my %hash; while (<DATA>) { my @splits = split; my $snp = $splits[0]; $counter++ if not exists $hash{$snp}; print "$counter $snp\n"; $hash{$snp}++; } __DATA__ foo abc foo cde bar xyz

Prints:

1 foo 1 foo 2 bar

Replies are listed 'Best First'.
Re^2: only increment counter if the ID has not been seen before
by martin (Friar) on Mar 12, 2015 at 10:18 UTC
    The OP wanted the same number in front of each occurence of the same ID, not a slowly increasing number counting different IDs so far. This can be achieved with a hash storing unique numbers rather than repetition counts.
    use warnings; use strict; my $counter = 0; my %hash; while (<DATA>) { my @splits = split; my $snp = $splits[0]; $hash{$snp} ||= ++$counter; print "$hash{$snp} $snp\n"; } __DATA__ foo abc foo cde bar xyz foo hij
    This should output:
    1 foo 1 foo 2 bar 1 foo
    ... rather than:
    1 foo 1 foo 2 bar 2 foo
Re^2: only increment counter if the ID has not been seen before
by Anonymous Monk on Mar 12, 2015 at 01:21 UTC
    Ah, I just needed this thing then: $hash{$snp}++;
    Damn! Thank you very much!