ahjohnson2 has asked for the wisdom of the Perl Monks concerning the following question:
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;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Compare 2 Hashes w/ Multiple Variables
by Laurent_R (Canon) on Dec 23, 2013 at 22:24 UTC | |
by Tanktalus (Canon) on Dec 24, 2013 at 17:28 UTC | |
by Laurent_R (Canon) on Dec 24, 2013 at 18:07 UTC | |
|
Re: Compare 2 Hashes w/ Multiple Variables
by PerlSufi (Friar) on Dec 23, 2013 at 20:20 UTC | |
|
Re: Compare 2 Hashes w/ Multiple Variables
by Laurent_R (Canon) on Dec 23, 2013 at 19:58 UTC |