in reply to How can I keep the first occurrence from duplicated strings?
The defined or assignment operator only assigns the right hand value if the value to the left is undefined so you can use that to assign just the first value found to a hash:
use strict; use warnings; my $input = <<IN; nick 5 nick 10 nick 20 john 78 erik 9 erik 12 IN my %hits; open my $fin, '<', \$input; while (my $line = <$fin>) { next if $line !~ /^(\w+)\s+(\d+)/; $hits{$1} //= $2; } print "$_: $hits{$_}\n" for sort keys %hits;
Prints:
erik: 9 john: 78 nick: 5
|
---|