#UNTESTED use strict; use warnings; my %files = ( FOO => { A => 2 }, BAR => { B => 3 } ); MAIN: while () { my ($col1, $col2) = split m/\s+/; foreach my $file (keys %files) { if (exists $files{$file}{$col2} ) { #do something; next MAIN; } } } #### ##ALSO UNTESTED while () { my ($col1, $col2) = split m/\s+/; stuff_i_need_do($col1) if ( check_for_condition(\%files,$col2) ); } sub check_for_condition { my $files = shift || return undef; my $col = shift || return undef; foreach my $file ( keys %{$files} ) { return 1 if (exists $files{$file}{$col2} ); } return undef; } sub stuff_i_need_to_do { my $stuff = shift; #do something; }