use strict; use warnings; my (%h, $handle); open($handle, 'file1.txt'); while (<$handle>) { # Creates hash using first field of each line as keys $h{(split /,/)[0]} = (); } close($handle); open($handle, 'file2.txt'); while (<$handle>) { # Prints second field of each line if it exists in the hash chomp; print "$_\n" if exists $h{$_ = (split /,/)[1]}; } close($handle);