From: "Jesús Gabriel y Galán" Date: 2013-07-24T15:50:32+09:00 Subject: Re: how to create structure like bellow On Wed, Jul 24, 2013 at 12:14 AM, Expert Alart wrote: > Hi > thanks you are right, i might be thinking too hard .. may be it can be > done easily ... but still i am in puzzle ... > > > The value will come from a file , its has raw data like bellow > > xx-xx-xx-xx-a-z16d1 > xx-xx-xx-xx-b-z16d1 > ................................... > xx-xx-xx-xxx-b-z15a7 > xx-xx-xx-xxx-b-z15a7 > ................................... > > > So to avoid mistakes > i will have a manual entry , example there will be 8 Groups > z16,z15,z1f........z1d So, the "identifier" of the row, are those three characters that come 2 before the end? If so, you can extract them easily witha regex: 2.0.0p195 :002 > id = "xx-xx-xx-xxx-b-z15a7".match(/(...)..$/).captures[0] => "z15" > as you can see those lines in file has those entry > > so i need to have like this > z16 > xx-xx-xx-xx-a-z16d1 > xx-xx-xx-xx-b-z16d1 > z15 > xx-xx-xx-xxx-b-z15a7 > xx-xx-xx-xxx-b-z15a7 > > Now run a loop to do snmp like > > for z15 , go each list > power=`snmp xx-xx-xx-xxx-b-z15a7` > total=power+toal > > end > > > so the same for rest 8 .. > to make it simple ... > i am just planning to have array where i will insert those number > manually.. > object={z16,z15....z1d} > > now > > object.each do |obj| > Read from the file > if line entry has "z16" > power=`snmp xx-xx-xx-xx-a-z16d1` > power_total=power+total_power > > end > store this value into z16 > > end > > > So when i will represent the value in report it will be > > Total power for Z16=12 > Total power for Z13 =12 > .................................. > ........................... > > may be can be done more simple There's a very neat trick with hashes by which you can set a default value for a Hash, so when you access a key that doesn't exist, it returns that value. This lets you do things like this pretty easily: h = Hash.new(0) # h will return 0 for non existing keys h["z16"] += 3 this is equivalent to h["z16"] = h["z16"] + 3 and as h has a default value of 0, the second h["z16"] will return 0 the first time, adding 3 and storing it. So you could do it like this (mix of pseudocode and code): totals = Hash.new(0) for each line of the file id = line.match(/(...)..$/).captures[0] power=`snmp #{line}` totals[id] += power end Hope this helps, Jesus.