From: "Jesús Gabriel y Galán" Date: 2013-07-26T18:32:48+09:00 Subject: Re: how to create structure like bellow On Fri, Jul 26, 2013 at 10:48 AM, Expert Alart wrote: > Hi Jesus > > Thanks its works perfect.. > but i have a doubt and want a explanation ... > its looks like there is an issue with this > > totals[id] += power > > its missing couple of entry .... > > example , > > Total Power Per Row: > z15a :- 192 > z16d :- 131 > z16e :- 179 > z16f :- 108 > > Total Power Uses at this DC : > AMP : 629 > > but if i 192+131+179+108 : 610 > but when i calculate manualy the all amp i get : 629 ... > > > so there is a mismatch .... > > how is this totals[id] += power working ?? Well, as I explained before, this expression is equivalent to this one: totals[id] = totals[id] + power As we have defined 0 as the default value for the hash, totals[id] + power is 0 for the first time you see that key. Check this example in IRB: 2.0.0p195 :001 > h = Hash.new(0) => {} 2.0.0p195 :002 > h["z15"] => 0 2.0.0p195 :003 > h["z15"] + 3 => 3 2.0.0p195 :004 > h["z15"] += 3 => 3 2.0.0p195 :005 > h => {"z15"=>3} 2.0.0p195 :006 > h["z15"] += 5 => 8 2.0.0p195 :007 > h => {"z15"=>8} As you can see, everytime you do h[id] += power, if the key is not found, 0 is used, summed to power and stored. The next time, the key is found and power is summed to whatever was there before and stored. Regarding the mismatch, how do you get this "Total Power Uses at this DC : AMP : 629"? Is this manual thing really extracting the same data? Is the data returned by the snmp command static? Maybe when you calculated it manually, the data already changed. We need more info about the mismatch to be able to help you. Jesus.