File 1 contains: name,metric1,metric2,metric3. File 2 contains: name,metric4. I have a script in place that will compare the two "names" and find a match. However, I also want to print metrics 1-4. Code looks like this:

foreach $key (keys %hash1) { if (exists $hash2{$key}) { print "$key\n"; } }

Am somewhat new to perl and can get syntax down to print the $key that is common to both files, but also want to print metrics 1-4 on each line. How to include metrics 1-4 on same line as $key Would appreciate any assistance.

here's all the code

$filename1 = shift; $filename2 = shift; open (IN1, "$filename1"); open (IN2, "$filename2"); %hash1; %hash2; @data1 = <IN1>; foreach $line1 (@data1) { # print "$line1\n"; chomp $line1; $line1 =~ /(.*?)\,(.*?)\,(.*?)\,(.*?)\,(.*?)\,(.*)/; $description1 = $1; $cktsizetemp1 = $2; $bits1 = $3; # print "$3\n"; $availability1 = sprintf("%.2f", $4); $bitsin1 = $5; $bitsout1 = $6; $description1 =~ /.*?\-(.*?)\-(S|G|SD|A|AD|FAA|SA).*?([A-Z|0-9 +][A-Z|0-9][A-Z|0-9][A-Z|0-9])$/; $devicename1 = "$1\-$2"; $cd1 = $3; # print "$devicename1\n"; $hash1{$devicename1} = "$devicename1"; } @data2 = <IN2>; foreach $line2 (@data2) { # print "$line2\n"; chomp $line2; $line2 =~ /(.*?)\,(.*)/; # print "$line2\n"; $description2 = $1; # print "$description2\n"; $cpu_util = $2; $description2 =~ /.*?\-(.*?)\-(S|G|SD|A|AD|FAA|SA).*?(.|:)/; # print "$description2\n"; $devicename2 = "$1\-$2"; $cd2 = $3; # print "$devicename2\n"; $hash2{$devicename2} = "$devicename2"; } #####good to here##### foreach $key (keys %hash1) { if (exists $hash2{$key}) { print "$key\n"; } } close IN;

In reply to Compare 2 Hashes w/ Multiple Variables by ahjohnson2

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.