foreach $sic (keys %hash){ next if !$hash{$sic}{BUS}; print "str1 = $str[1] sic= $sic, cat= $hash{$sic}{CAT} bus= $hash{$sic}{BUS}\n"; if ( $str[1] =~ m/$hash{$sic}{BUS}/){ $str[38] = $hash{$sic}{CAT}; $str[39] = $sic; print "Found one. Bus name is $str[1] and cat is " . $hash{$sic}{CAT} . " and sic is " . $str[39] ."\n\n\n"; } }