# the top loop as before, then the bottom loop looks like this foreach my $message_key (keys %relayqid_hash) { my $result_tab = $relayqid_hash{$message_key}; my $subject = ($result_tab->{'Subject'}) ? $result_tab->{'Subject'} : "[no subject]"; insert_hash($result_tab); }