开发者

Manipulation of hash and arrays in Ruby

开发者 https://www.devze.com 2023-01-11 13:17 出处:网络
For my first question here I would like to ask you how you\'d do the following in Ruby. I have a hash with the following aspect

For my first question here I would like to ask you how you'd do the following in Ruby.

I have a hash with the following aspect

variables["foo"] = [1,2,3]
variables["bar"] = [4,5,6,7]
variables[...] = ...

Update: this hash will have an arbitrary number of key => values pairs.

So it represents parameters and their possible values. I would like to "generate" now an Array containing hashes whose key=>value pairs represent each possible combination of the variables. In the case of the example above, I would have an array of 12 (=3x4) hashes like that

[ hash1, hash2, ..., hash16]

where hashi would be

 hash1["foo"] = 1
 hash1["bar"] = 4
 hash2["foo"] = 1
 hash2["bar"] = 5
 hash3["foo"] = 1
 hash3["bar"] = 6
 hash4["foo"] = 1
 hash4["bar"] = 7
 hash5["foo"] = 2
 hash5["bar"] = 4
 hash6["foo"] = 3
 hash6["bar"] = 4
 ...
 hash16["foo"] = 3
 hash16["bar开发者_JS百科"] = 7

I have a few ideas but all of them are quite complicated nested loops ...

Thanks a lot !


vars = {foo: [1, 2, 3], bar: [4, 5, 6, 7]}

(v = vars.map {|k, v| ([k] * v.length).zip(v) }).first.product(*v.drop(1)).
map {|args| args.reduce({}) {|h, (k, v)| h.tap {|h| h[k] = v }}}
# => [{:foo=>1, :bar=>4},
# =>  {:foo=>1, :bar=>5},
# =>  {:foo=>1, :bar=>6},
# =>  {:foo=>1, :bar=>7},
# =>  {:foo=>2, :bar=>4},
# =>  {:foo=>2, :bar=>5},
# =>  {:foo=>2, :bar=>6},
# =>  {:foo=>2, :bar=>7},
# =>  {:foo=>3, :bar=>4},
# =>  {:foo=>3, :bar=>5},
# =>  {:foo=>3, :bar=>6},
# =>  {:foo=>3, :bar=>7}]

This works with arbitrary many entries and arbitrary keys.


Ruby syntax for an Array is brackets, and for a hash is curly braces with a key, hash rocket => value.

variables = { "foo" => [1,2,3] , "bar" => [4,5,6,7] }

results = Array.new

variables["foo"].each do |foo_variable|
  variables["bar"].each do |bar_variable|
    results << { "foo" => foo_variable , "bar" => bar_variable }
  end
end

require "pp"
pp results


vars.values.inject(&:product).map{|values|
  Hash[vars.keys.zip(values.flatten)]
}
0

精彩评论

暂无评论...
验证码 换一张
取 消