# read file1 into a %hash ... code to do that here ... # inside a loop while (my $line = <$file2>) { # read file2 line by line ... this was done in the loop condition above ... # split the $line to @fields at | # if the first $fields[0] exists in the %hash, # print the whole $line to file 3 }