foreach (@first_file) { #get data from first file /\b(\w+)\b(.*)/; #split the first record, by name and data ($key, $data) = ($1, $2); #put thoose into scalars foreach (@second_file) { #next file foreach line in first file /\b(\w+)\b(.*)/; #run through same regex and get keys again if ($1 eq $key) { #compare keys from both files $merge{$1} = "$data$2"; #if they match, merge into hash } } }