# How to create structure like bellow

Hi…

I need a structure like bellow , i know its not correct syntax, but just
trying to give an idea so that i can get a correct answer …

row value :

a-z15a6=5
b-z15a6=4
a-z15a7=3
b-z15a7=3

a-z16af=3

I need to store Value in this…
z15a={[a-z15a6=>5,b-z15a6=>4],[a-z15a7=>3,b-z15a7=>3],…n}
z16a={[a-z16af=>3,b-z16af=>4],[a-z16ag=>3,b-z16ag=>3],…n}

possible like this

[z15a=>{[a-z15a6=>5,b-z15a6=>4],[a-z15a7=>3,b-z15a7=>3],…n}},z16a=>{[a-z16af=>3,b-z16af=>4],[a-z16ag=>3,b-z16ag=>3],…n}]

then i need to calculate value from this example
total of z15a = 5+4+3+3 (top row)
Total of z16a = 3+ 4 +3 +3 (bottom row)

but dont understand whats the logic will i use …

Can any one please give me some light

Thanks

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

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

On Jul 23, 2013, at 2:40 PM, Expert A. [email protected] wrote:

Hi…

I need a structure like bellow

“Bellow” is to shout like an elephant.

“Below” means comes after.

but dont understand whats the logic will i use …

Can any one please give me some light

Thanks

Posted via http://www.ruby-forum.com/.

Where is the data coming from? A file? A spreadsheet? A database? This
information will aid greatly in giving you a possible answer. What you
have above is rather complicated, and it seems you would make your life
a lot simpler if you made it less complex.

For example, you say your desired outcome is the sum of the values of
each hash element in each row. I’m think there must be a much easier way
to organize this rather than drilling through the above data structure.
Please show where the information comes from; maybe it’s simpler than
you’re thinking.

On Wed, Jul 24, 2013 at 12:14 AM, Expert A. [email protected]
wrote:

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”

Now run a loop to do snmp like
i am just planning to have array where i will insert those number

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.

On Fri, Jul 26, 2013 at 10:48 AM, Expert A. [email protected]
wrote:

example ,
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

Jesus.

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 ??

I think i know why …

let me try to explain

when i am doing snmp on 2 pdues… ( directly command line not from ruby
)

actuall value i get is :

3.60
4.60

So it will be : 8.20

group_by_row[get_row] += row_per_power_amp.to_i :

its getting value : 7

and my way its getting : 8

so both way its not right …
and we have 82 pdues so we losing amps on report …

bellow is the code :

pdus.each do |pdu_name|
puts pdu_name[:name]
get_row=
pdu_name[:name].match(/.(\w\d\d\w).\$/).captures[0]

``````                                            puts "<br>"
#total_power=0
power=
``````

`/usr/bin/snmpwalk -v 2c -OQ -c xxxx #{pdu_name[:name]} xxxxxxxx | awk '{print \$3}'`
row_per_power_amp=(power.to_i/10)

# This is for “group_by_row”

``````                                            total_power=total_power.to_i
``````
• power.to_i # This is for manual output

``````                                          group_by_row[get_row] +=
``````

row_per_power_amp.to_i

``````                                            end

total_power_amp=(total_power.to_i/10)

total_power_amp_dc=total_power_amp_dc.to_i
``````
• total_power_amp.to_i

``````                                  puts "<br>"
puts "<u>Total Power Uses Per
``````

racks (amps): #{total_power_amp} amps"

``````                            end

total_power_amp_per_dc=(total_power_amp_dc.to_i/10)
Total Power for DC:
``````

#{total_power_amp_per_dc}

i think they way i am storing values from snmp is not right … (
floating point entry )

am i right ???

Hi
i got this now