in reply to Compare two file text input, compare it, replace and write new file

To populate your hash, change

$words{$key} .= exists $words{$key} ? "$val" : $val;

to

$words{$key} = $val;

-- Ken