perl matching two columns from different tables -
i have table 3 columns, called table1
1 2 2 3 b 4 5 d i have table 1 column, called table2, looking this
a b e f if there match between table2 , third column of table1, want print matching line new table called output, in case should this
1 2 2 3 b i'm writing in perl , think should using hash. tried following
#!/usr/bin/perl use strict; use warnings; open(my $table1,'<',"table1.txt"); open(my $table2,'<',"table2.txt"); open(my $output,'+>',"output.txt"); %hash = (); while (<$table2>){ chomp; $keyfield = $_; push @{$hash{keyfield}}; } seek $table1,0,0; while (<$table1>){ chomp; @cols = split(/\t/); $keyfield = $cols[2]; if (exists($hash{$keyfield})) { print $output $_, "\n"; } } this approach worked before, had modify slightly. warning: useless use of push no values @ line13 (which line push is). , output empty. doing wrong?
you need use $hash{$keyfield} = 1 create association instead of push.
per tip @sobrique, can use $hash{$keyfield}++ let determine later whether key exists , give number of occurrences.
Comments
Post a Comment