foreach my $host_results (@$results) { foreach my $result_tab (@$host_results) { my $uniquekey = $result_tab->{'HostName'}.$result_tab->{'Qid'}; if ($relayqid_hash{$uniquekey}) { # if the relayqid exists ignore my $existing_Recipient = $relayqid_hash{$uniquekey}->{'Recipient'}; } else { # create the hash record with the result_tab in it $relayqid_hash{$uniquekey}= $result_tab; } } foreach my $host_key (keys %relayqid_hash) { foreach my $result_tab (@$host_key) { my $subject = ($result_tab->{'Subject'}) ? $result_tab->{'Subject'} : "[no subject]"; # do other stuff not important insert_hash($result_tab); } }