The syntax you showed on your previous message looks fine to just print the row that matches.
In this message, though, you're saying you want to print the same values from both tables. To do that, you can use:
print $fields[0], " ", $row if exists $hash{$fields[0]};
It's redundant, though, since the first column in $row (i.e. $fields[0]) is also the same value as you had in the first table.
Suppose, however, your first file contained the key *and* some values, like the second file does. In that case, you need to change your first loop that loads up the hash. You'd split it on the delimiter just like you do for the second file, and then rather than just using $hash{$line} = 1, you'd store the value(s) you want, like:
my %hash=(); open (my $fh,'<',$file2) or die $!; while(my $line=<$fh>) { chomp $line; # Split the line into fields (first field is the key) my @fields = split /\|/, $line; # store the entire line in the hash under the key $hash{$fields[0]} = $line; } close $fh; print "KEYS AND RECORDS ARE:\n"; print Dumper(\%hash);
Once you have all your data in your hash, then you can print both rows when the keys match:
print $hash{$fields[0]}, " ", $row if exists $hash{$fields[0]};
...roboticus
When your only tool is a hammer, all problems look like your thumb.
In reply to Re^11: Filtering Output from two files
by roboticus
in thread Filtering Output from two files
by vighneshmufc
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |