Hello everyone, I'm obviously new and have a problem understanding the right tool to use to make mathematical comparisons between the values of two different hashes that (should) have the same keys.

I have many point to multi-point radios at work, and the signal strengths can be tested by using telnet. I hacked a script together using expect to get on the radios in question and run the tests, then used shell tools like grep and cut to get the data reduced to just what (I think) I need.

The data ends up in a hash, and it has this form:

%hash1 = ("1", "20", "2", "20", "4", "19", "5", "20", "6", "18"); %hash2 = ("1", "19", "2", "20", "4", "16", "5", "19", "6", "20");

I've read through google about tools (Data::Compare) that will compare the entire hash and report if they are identical or not, but that isn't what I'm looking to do. I know that except for one time in 1000, %hash1 and %hash2 won't be identical.

What I'm looking to accomplish is to have perl look at each key/value in the first hash, and simply compare it to the same key/value in the second.

The results I'd like to see based on the example above is something like:

1 (20-19)=1 2 (20-20)=0 4 (19-16)=3 5 (20-19)=1 6 (18-20)=-2

What I'm not grasping is how to get perl to compare the individual key->value to the second key->value.

I probably made it seem like I'm looking for the output to be a 3rd hash. That wouldn't be bad, but it also isn't necessary, all I need is the difference in scores. The examples are real values, everything in the hashes are numbers, there aren't any strings. The numbers do get a bit uglier (the second hash is made up of the averages of several tests).

Its also distinctly possible that I'm going about this in completely the wrong way, and if so please point me in the right direction!

Thanks!


In reply to How best to compare hash values? by jimbass

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.