my %hash = ( # the hash you demonstrated ); foreach my $name ( keys %hash ) { print "$key $hash{$key}{detection_profile}", exists $hash{$key}{detection_profile_severity} ? " $hash{$key}{detection_profile_severity}\n" : "\n"; }