use strict; use warnings; use DBM::Deep; my $db_filepath = 'lookup.db'; my $file1 = 'XXXX.txt'; my $file2 = 'YYYY.txt'; my $db = DBM::Deep->new($db_filepath); open(my $fh, $file1) or die "[Error] COULD NOT OPEN FILE [$file1]-[$!]"; while (<$fh>) { my $line = $_; # get a common key from the data somewhere in here my @fields = split (/\s/ ,$line); my @output = grep /rs\d{5,}\b/ ,@fields; my $rs = join (':' , @output); $rs =~ s/:/\n/g; $db->{'some-common-key-bewtween-files'} = $rs; } close($fh); # now iterate through your other file and lookup using DBM::Deep open(my $fh2, $file2) or die "[Error] COULD NOT OPEN FILE [$file2]-[$!]"; while (<$fh2>) { my $line = $_; if ($line =~ /some-common-key-bewtween-files/) { my $db_record = $db->{'some-common-key-bewtween-files'}; # now you have linked data from both files # do your other coding here. } } close($fh2);