Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2024-04-20 02:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found