http://www.perlmonks.org?node_id=458213


in reply to combining 2 files with a comon field

<perl newbie puts head above parapet for first time - cautiously - to humbly offer an approach that doesn't use arrays (so it can handle BIG files) and ignores keys only present in one of the input files>
#!/usr/bin/perl -w use strict; open ONE, "1.txt" or die "Cannot open 1.txt to read\n $!"; open TWO, "2.txt" or die "Cannot open 2.txt to read\n $!"; open TRE, ">3.txt" or die "Cannot open 2.txt to write\n $!"; while (<ONE>) { chomp; (my $onea, my $oneb) = split(/\|/); my $twoa = undef; my $twob = undef; while (! eof(TWO)) { my $two = <TWO>; chomp $two; ($twoa, $twob) = split(/\|/, $two); last if ($twoa ge $onea); } if ($onea lt $twoa) { next; } else { print TRE "$onea\|$oneb\|$twob\|\n" if ($onea eq $twoa); } }