se strict; use warnings; sub get_file { open my $FILE, '<', shift or die $!; return map {chop; $_ => $_} <$FILE>; } my %a = get_file 'file1.txt'; my %b = get_file 'file2.txt'; { no warnings 'uninitialized'; print "$_\n" for grep {$_} @a{keys %b}; }