Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Comparing two associative arrays

Status
Not open for further replies.

plasmatwin

Programmer
Nov 1, 2005
31
GB
Hi,

I have my configuration loaded into a hash called %config, I want to check that hash for the presence of certain mandatory values. It is currently a hash of two hashes, I want to make another temporary hash of two hashes with some default values in it and then loop through each value of the temporary hash and check that there is a value for it in the main hash called %config (and if there isn't a value for it, create it with the default value).

I'm in the dark about this one, so can anyone help me out?

Thanks,
Graham
 
Yep, sounds simple in principle but not so easy to explain.
I'll try to show you.
Code:
while(($key,$value) = each %{$mandatory{'leftHalf;}}) {
  $config{'leftHalf'}{$key} = $value
    unless $config{'leftHalf'}{$key};
}
I've not tested it but the principle should be roughly right.
Also I've used "leftHalf" for the half of the sub hash that you want to use. Replace as you see fit.

Trojan.


Trojan.
 
That's cool, pretty much exactly what I was looking for. Does it require that I specify "lefthalf"? Because there are multiple sections and I was hoping for a "one size fits all" algorithm that could compare any style of hash format (with as many dimensions that were neccessary and stuff...). I'm guessing that would be pretty complex though [ponder]
 
hm... well, say that I have a nice complicated hash of hashes that looks like so on decleration:
Code:
my %config = (
  "yesh" => {
    "yarr" => "harr",
    "chocolate" => 0,
    "fruit" => {
      "banana" => "yellow",
      "orange" => "well... orange",
    },
  },
  "boo" => {
    "something" => "value",
    "erk" => "erg"
  },
  "last" => "value",
);
Obviously that's not the real array, but it's the awkward shape of it that counts. :)
Now, if the values $config{'last'}, $config{'boo'}{'erk'} and $config{'yesh'}{'fruit'}{'orange'} were the bare minimum values, the mandatory array would look like so:
Code:
my %mandatory = (
  "yesh" => {
    "fruit" => {
      "orange" => "well... orange",
    },
  },
  "boo" => {
    "erk" => "erg"
  },
  "last" => "value",
);
Now how do I compare them and set values to defaults if neccessary, remembering that the mandatory values, the shape of the hash and anything else can change from one run to the next :p
 
You need to walk each element as a tree.
Check it with "ref" and see if the value is a reference to a hash and then walk the sub tree.
It's a bit hairy for me to spend much more time with now.


Trojan.
 
well, if your hash can change from one run to the next, there is no way to check. How will you know what your mandatory key/value pairs are?
 
The mandatory hash is declared in the source code pretty much as it is there. The point is the mandatory one can be changed by me expanding the source and I didn't want the reply "just check each value manually one by one" ;)

Trojen: thanks for the tip on where to start, I'll post back once I have something.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top