I would agree with
ruzam that a recursive function is the way to handle this, given that the hashes can differ at any intermediate level of the structure, and a path that goes down 7 layers in one hash simply does not exist beyond the fourth layer in the other hash.
As for his remark about the problem with your sample data, I would take that a little further -- I wasn't really sure what your criteria are for the particular values you want as output:
- the first came from "old_values", and was the lower of the two having the same hash keys (14 vs. 15)
- the second came from "new_values", and was the lower of the two having the same hash keys (19 vs. 21)
Was that really your intention, or just a typo in the OP?
Anyway, I wonder if your hash structure really needs to have so many layers. Is there anything besides "MachineType" at the top level? Anything besides "Unit" at the third level? In other words, couldn't the structure really be like this:
$values{Blue_machine}{2}{0}{19}
$values{Red_machine}{1}{0}{22}
...
For that matter, you could flatten this out, and make the comparisons trivially simple, by just concatenating the keys into a single string, instead of using them as separate layers:
$values{"Blue_2_0_19"}
$values{"Red_1_0_22"}
...
How are you loading up the hash stuctures in the first place? (An xml parse? I suppose it might be easier in some sense to stick with the deep structure if that's the case, though I'd still look for a way to refactor the parsing to allow a flatter data structure.)
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.