Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Re: Compare 2 Hashes w/ Multiple Variables

by Laurent_R (Canon)
on Dec 23, 2013 at 22:24 UTC ( #1068269=note: print w/replies, xml ) Need Help??

in reply to Compare 2 Hashes w/ Multiple Variables

Hi ahjohnson2,

OK, now you have added your code. What you are doing is far far too complicated. What you need to do is this:

- Read file1 and, for each line, find the name (the key) and the three metrics, and store in your hash something like this:

$hash1{$key}= "$metric1;$metric2;$metric3";
- Then, you read file2 line by line, split each line to find the name2 and mtric4. Once you have that, check is the name2 is an existing key of %hash1, and, if such is the case, just print the output with something like this:
print $name2, $hash{$name2}, ";", $metric4, "\n" if exists $hash{$name +2};
No need for %hash2, no need to store the file in an array.

Also, I have not seen your input data, but this code:

$line1 =~ /(.*?)\,(.*?)\,(.*?)\,(.*?)\,(.*?)\,(.*)/; $description1 = $1; $cktsizetemp1 = $2; $bits1 = $3; # print "$3\n"; $availability1 = sprintf("%.2f", $4); $bitsin1 = $5; $bitsout1 = $6;
could be replaced by something much simpler like this:
my ($description1, $cktsizetemp1, $bits1, $availability1, $bitsin1, $b +itsout1) = split /,/, $line1; $availability1 = sprintf("%.2f", $availability);
Finally, I would suggest that you use strict; and use warnings; near the top of your program and declare your variables with the my function.

Please provide a sample of your input data (file1 and file2) if you need further detailed help.

Replies are listed 'Best First'.
Re^2: Compare 2 Hashes w/ Multiple Variables
by Tanktalus (Canon) on Dec 24, 2013 at 17:28 UTC
    $hash1{$key}= "$metric1;$metric2;$metric3";

    Personally, I'm not a fan of putting special characters anywhere. Or merging data together whenever and wherever it's not strictly required. Need to track information in memory? Merging not required. Need to send it to a file or to a remote host? Required. But then you use a nice serialisation protocol, such as JSON, XML, Storable, or even xSV (using Text::CSV_XS - it would at least handle cases where the metrics may have embedded semicolons).

    So I'd want to keep each metric separated out. This also makes it easier to discern if file two has a repeated metric, in case you want to merge only unique metrics.

    But maybe I'm just overthinking things :)

      Well, sure, I fully appreciate your point, but, to start with, this is just a brief simple example, which would have to be adapted to the exact needs. My point was really to show the algorithm: store the content of one file in a hash and then read the second file and use the data stored before. Storing the data in the hash can be done in many many different ways (hash of arrays, hash of hashes, XML, CSV, fixed-length, etc.), I just chose a very simple way because this was not really the subject of the post (wghich was the algorithm to do the job).

      Then, the OP said he just needed to print out metrics 1 to 4; merging the data before storing into the hash makes things as simple as they can be when it comes to print them out. Any decision to make things more complex would just complicate the explanation of what needs to be done and would be actually meaninglessly premature so long as you haven't seen any of the data.

      And, BTW, I received an MP from the OP saying: "Thanks so much! It works just like I wanted!"

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1068269]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (11)
As of 2018-03-21 16:51 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (270 votes). Check out past polls.