in reply to print data with matching id from two different files

I tried a new code and realized that file2 (similarly file3)contains more than 18000 rows and it has repeating values of ids and names

I tried following code to get desired output:

#!usr/bin/env perl #use strict; use warnings; open (FILE1,"< file1”); while (!eof(FILE1)) { chomp ($element1 = (<FILE1>)); open (FILE3"< file3”); while (!eof(FILE3) { $element2 = (<FILE3>); if ($element2 =~ /$element1/) {print $element2;} } }

This code is matching but printing values in replicates (for example: output has ABS0057 BILL = 3 times, ABS0056 SAM = 3 times) since file3 itself contain replicate values. I want to print one in output that shows match (say ABS0057 from file1 matches ABS0057 BILL from file3, output should print just one ABS0057 BILL)

Replies are listed 'Best First'.
Re^2: print data with matching id from two different files
by hippo (Archbishop) on Nov 02, 2017 at 12:05 UTC
    #use strict;

    Never do this. Never comment out strict. If your script only compiles without strict, put it back in and fix all the errors first before doing anything else. strict is there to stop you making mistakes.

      #!usr/bin/env perl use strict; use warnings; open (FILE1,"< file1"); while (!eof(FILE1)) { chomp (my$element1 = (<FILE1>)); open (FILE3,"< file3"); while (!eof(FILE3)) { my$element2 = (<FILE3>); if ($element2 =~ /$element1/) {print $element2;} } }