in reply to Re^8: Filtering Output from two files
in thread Filtering Output from two files

thank you i am able to do it
now i wanna print the matching lines
print $row if exists $hash{$fields[0]}; something wrong with this syntax ?

Replies are listed 'Best First'.
Re^10: Filtering Output from two files
by Anonymous Monk on Feb 06, 2018 at 09:22 UTC
    i have excluded the part where i load the files to a hash i just wanna print the same values from both table

      vighneshmufc:

      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.