# Put this after defining %ItemUID. my %hvalues; # instead of my @hvalues. ... if (not exists $ItemUID{$uid}) { $ItemUID{$uid} = $src } # At end of first while loop, insert this line after the one above. if (not exists (%hvalues{src})) { %hvalues{src} = 1; }