use strict; use warnings; use feature 'say'; my %hash = ( file0 => { opt0 => undef, opt1 => 1 }, file1 => { opt0 => 0, opt1 => 1 }, ); foreach my $file ( keys %hash ) { my %temp = %{ $hash{$file} }; OPTION: foreach my $opt ( keys %temp ) { if ( !exists $hash{$file} or !exists $hash{$file}{$opt} or !defined $hash{$file}{$opt} ) { warn "Option does not exist\n"; next OPTION; } $hash{$file}->{$opt} =~ s/[^[:ascii:]]//gxms; my $lined = join( "\t", $file, $opt, $hash{$file}->{$opt} ) . "\n"; } }