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.


In reply to Re^11: Filtering Output from two files by roboticus
in thread Filtering Output from two files by vighneshmufc

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.