# N00b ? re: CGI Hash and arrays

**URL:** <https://www.ruby-forum.com/t/n00b-re-cgi-hash-and-arrays/167053>\
**Category:** Ruby\
**Created:** [May 2, 2009, 10:27pm UTC](https://www.ruby-forum.com/t/n00b-re-cgi-hash-and-arrays/167053 "2009-05-02T22:27:54Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeffl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ea666f/32.png) [@jeffl](https://www.ruby-forum.com/u/jeffl)\
**Post date:** [May 2, 2009, 10:27pm UTC](https://www.ruby-forum.com/t/n00b-re-cgi-hash-and-arrays/167053/1 "2009-05-02T22:27:54Z")

</div>

Hi, n00b here, and got myself stumped on something I thought was easy.

I get CGI params returned as:

params: {“conf\_mod”=\>[“20”], “conf\_impact”=\>[“10”],  
“avail\_impact”=\>[“5”], “integrity\_impact”=\>[“5”],  
“authorization”=\>[“5”], “integrity\_mod”=\>[“5”]}

I just want to add up the values in the hash keys. Seems simple, but an  
hour or so later, after trying numerous things no go. Any help  
appreciated.

#!/usr/bin/ruby

require “cgi”

cgi = CGI.new(‘html4’)  
values = cgi.params  
#values.each\_value { |y| total += (eval y.join(’+’)) }  
#values.each\_value { |x| total += x[0] }  
#values.each\_value do |v| puts v end  
#cgi.header  
cgi.out() do  
cgi.html() do  
cgi.head{ cgi.title{“TITLE”} } +  
cgi.body() do  
cgi.pre() do  
CGI::escapeHTML(  
"params: " + cgi.params.inspect + “\n”  
"total: " + puts(total) + “\n”  
)  
end  
end  
end  
end

---

<div class="post-metadata">

**Author:** ![jeffl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ea666f/32.png) [@jeffl](https://www.ruby-forum.com/u/jeffl)\
**Post date:** [May 2, 2009, 10:45pm UTC](https://www.ruby-forum.com/t/n00b-re-cgi-hash-and-arrays/167053/2 "2009-05-02T22:45:31Z")

</div>

On May 2, 2009, at 4:27 PM, Jeff Leggett wrote:

> hour or so later, after trying numerous things no go. Any help  
> appreciated.

So you want to end up with total == 50, right?

The values are arrays of strings, so you need to sum up the (single)  
values after turning them to integers.

total = 0  
values.each\_value {|v| v.each {|s| total += s.to\_i } }  
puts “total: #{total}”

irb\> total = 0  
=\> 0  
irb\> values.each\_value {|v| v.each {|s| total += s.to\_i } }  
=\> {“conf\_mod”=\>[“20”], “conf\_impact”=\>[“10”], “avail\_impact”=\>[“5”],  
“integrity\_impact”=\>[“5”], “authorization”=\>[“5”],  
“integrity\_mod”=\>[“5”]}  
irb\> puts “total: #{total}”  
total: 50  
=\> nil

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![jeffl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ea666f/32.png) [@jeffl](https://www.ruby-forum.com/u/jeffl)\
**Post date:** [May 2, 2009, 11:04pm UTC](https://www.ruby-forum.com/t/n00b-re-cgi-hash-and-arrays/167053/3 "2009-05-02T23:04:57Z")

</div>

Jeff Leggett wrote:

> Hi, n00b here, and got myself stumped on something I thought was easy.
> 
> I get CGI params returned as:
> 
> params: {“conf\_mod”=\>[“20”], “conf\_impact”=\>[“10”],  
> “avail\_impact”=\>[“5”], “integrity\_impact”=\>[“5”],  
> “authorization”=\>[“5”], “integrity\_mod”=\>[“5”]}
> 
> I just want to add up the values in the hash keys.

The hash keys are the strings: “conf\_mod”, “conf\_impact”, etc. The hash  
values are the arrays.

> Seems simple, but an  
> hour or so later, after trying numerous things no go. Any help  
> appreciated.

params = {  
“conf\_mod”=\>[“20”],  
“conf\_impact”=\>[“10”],  
“avail\_impact”=\>[“5”],  
“integrity\_impact”=\>[“5”],  
“authorization”=\>[“5”],  
“integrity\_mod”=\>[“5”]  
}

total = 0

params.each do |key, val|  
num\_str = val[0]  
num = num\_str.to\_i  
total += num  
end

puts total

–output:–  
50

---

<div class="post-metadata">

**Author:** ![jeffl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ea666f/32.png) [@jeffl](https://www.ruby-forum.com/u/jeffl)\
**Post date:** [May 3, 2009, 12:01am UTC](https://www.ruby-forum.com/t/n00b-re-cgi-hash-and-arrays/167053/4 "2009-05-03T00:01:56Z")

</div>

What’s with inject?

params.inject(0) {|sum, (key, values)|  
sum + values.inject(0) {|a, b| a + Integer(b)}  
}

---

<div class="post-metadata">

**Author:** ![jeffl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ea666f/32.png) [@jeffl](https://www.ruby-forum.com/u/jeffl)\
**Post date:** [May 2, 2009, 11:13pm UTC](https://www.ruby-forum.com/t/n00b-re-cgi-hash-and-arrays/167053/5 "2009-05-02T23:13:28Z")

</div>

7stud – wrote:

> params.each do |key, val|  
> num\_str = val[0]  
> num = num\_str.to\_i  
> total += num  
> end

Once you understand that, you can make it briefer:

params.each do |key, val|  
total += val[0].to\_i  
end

…however if it’s possible that the arrays can contain more than one  
string, then you need to loop over each array too. As per Rob  
Biedenharn’s solution:

params = {  
“conf\_mod”=\>[“20”, “10”],  
“conf\_impact”=\>[“10”],  
“avail\_impact”=\>[“5”, “10”], #\<–xtra strings  
“integrity\_impact”=\>[“5”],  
“authorization”=\>[“5”, “10”],  
“integrity\_mod”=\>[“5”]  
}

total = 0

params.each do |key, val|

val.each do |str|  
total += str.to\_i  
end

end

puts total

–output:–  
80
