From: 7stud -- Date: 2009-05-03T06:04:59+09:00 Subject: n00b ? re: CGI Hash and arrays 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 -- Posted via http://www.ruby-forum.com/.