in reply to Substitute text string in a file with matching text from another file

Hashes are very useful for matching.

use strict; use warnings; my %lookup; { open(my $file1, '<', 'file1.txt') or die("..."); while (<$file1>) { chomp; my @fields = split(/\|/, $_); $lookup{$fields[0]}{$fields[1]} = $fields[2]; } } { open(my $file2, '<', 'file2.txt') or die("..."); while (<$file2>) { chomp; my @fields = split(/\|/, $_); if (exists $lookup{$fields[0]}{$fields[1]}) { $fields[3] = $lookup{$fields[0]}{$fields[1]}; } else { $fields[3] = 'NO MATCH'; } print(join('|', @fields), $/); } }

Tested.