my %HoH; while (my $row = $csv->getline_hr($data)) { my $key = "$row->{latitude}.$row->{longitude}"; # Compare next if exists $HoH{$key} && $row->{id} <= $HoH{$key}->{id}; # Set (or overwrite) $HoH{$key} = { name => $row->{name}, id => $row->{id} }; } # At this point I would return the hash (or a reference to it) but you would need to change the calling line, of course