in reply to merging content based on common columns of 2 files using grep

First of all, you should use strict; and use warnings; if you want to learn Perl the easy way

There are some errors in your code, some of them:

Here is a working version of your program:

use strict; use warnings; open(FH1,"file1.txt"); open(FH2,"file2.txt"); my @array=<FH2>; chomp @array; while(my $var=<FH1>){ chomp $var; my ($first,$second)=split(/\s+/,$var); my @grepped= grep {/\Q$second\E/} @array; print "$first\t$grepped[0]\n"; }

You should also consider using hashes to solve your problem

Another alternative, if the script is only going to join both files and output the result (and you are in a Unix/Linux system, as it seems to be the case) is to use bash's join tool:

$ join -1 2 -2 1 file1.txt file2.txt

citromatik

Replies are listed 'Best First'.
Re^2: merging content based on common columns of 2 files using grep
by heidi (Sexton) on May 06, 2009 at 15:04 UTC
    thank u :)