your code is behaving normally. hash keys are unique so chances are.. you have duplicate lines in your file.
in your loop, check for duplicates..
Code
my $dupe_count = 0;
while (my $line = <$fh>) {
chomp($fh);
my ($mouse, $human) = split /\s+/, $line;
if (exists $humanmouse{$human}") {
print "Duplicate line: $line";
$dupe_count++;
}
$humanmouse{$human} = $mouse;
}
close $f
print "Duplicates: $dupe_count" if $dupe_count;
or just check the uniq line count of your file versus the raw line count
Code
echo -n "Unique lines in file: "; uniq /location/of/file.txt | wc -l
echo -n "Total lines in file: "; wc -l /location/of/file.txt
if you want to have multiple 'mouse' values for a 'human', then you need to check if the key already exists and push the new $mouse value into $humanmouse{$human} as an array.
Code
while (my $line = <$fh>) {
chomp($fh);
my ($mouse, $human) = split /\s+/, $line;
if (my $existing_mouse = $humanmouse{$human}) {
if (ref $existing_mouse ne 'ARRAY') {
$humanmouse{$human} = ([$existing_mouse, $mouse]);
}
else {
push @{$humanmouse{$human}}, $mouse;
}
}
else {
$humanmouse{$human} = $mouse;
}
}
close $f
This post was edited by SLAMBOOZLED on Oct 3 2021 09:50am