带数组的表,返回哈希

时间:2015-03-24 18:11:40

标签: ruby hash

我有一个数组["1","1","a","b","y"]table1

table1

POSITION    NAME     VALUE     CODE
1           Animal   Dog       1
1           Animal   Cat       2
1           Animal   Bird      3
2           Place    USA       1
2           Place    Other     2
3           Color    Red       a
3           Color    Blue      b
3           Color    Orange    c
4           Age      Young     a
4           Age      Middle    b
4           Age      Old       c
5           Alive    Yes       y
5           Alive    No        n

对于数组中的每个元素,我想在POSITION中找到匹配的CODE - table1组合,其中POSITION是元素的索引(base 1)和CODE是数组中的元素。然后,我想返回一个哈希,其中包含匹配的相应NAMEVALUE作为键及其值。输出将是:

{"Animal" => "Dog", "Place" => "USA", "Color" => "Red", "Age" => "Middle", "Alive" => "Yes"}

我该怎么做?

1 个答案:

答案 0 :(得分:2)

我假设您要针对同一个表映射多个codes数组。假设您首先将表读入数组:

a = [["1", "Animal", "Dog", "1"],
     ["1", "Animal", "Cat", "2"],
     ["1", "Animal", "Bird", "3"],
     ["2", "Place", "USA", "1"],
     ["2", "Place", "Other", "2"],
     ["3", "Color", "Red", "a"],
     ["3", "Color", "Blue", "b"],
     ["3", "Color", "Orange", "c"],
     ["4", "Age", "Young", "a"],
     ["4", "Age", "Middle", "b"],
     ["4", "Age", "Old", "c"],
     ["5", "Alive", "Yes", "y"],
     ["5", "Alive", "No", "n"]] 

codes = ["1","1","a","b","y"]

然后你可以这样做:

codes.zip(a.chunk { |a| a.first }).map { |l,(_,b)|
  b.find { |c| c.last == l}[1...-1] }.to_h
  #=> {"Animal"=>"Dog", "Place"=>"USA", "Color"=>"Red",
  #    "Age"=>"Middle", "Alive"=>"Yes"} 

步骤:

enum0 = a.chunk { |a| a.first }
  #=> #<Enumerator:
  #   #<Enumerator::Generator:0x007f8d6a0269b8>:each> 

要查看枚举器的内容,

enum0.to_a
  #=> [["1", [["1", "Animal", "Dog", "1"], ["1", "Animal", "Cat", "2"],
  #           ["1", "Animal", "Bird", "3"]]],
  #    ["2", [["2", "Place", "USA", "1"], ["2", "Place", "Other", "2"]]],
  #    ["3", [["3", "Color", "Red", "a"], ["3", "Color", "Blue", "b"],
              ["3", "Color", "Orange", "c"]]],
  #    ["4", [["4", "Age", "Young", "a"], ["4", "Age", "Middle", "b"], 
  #           ["4", "Age", "Old", "c"]]],
  #    ["5", [["5", "Alive", "Yes", "y"], ["5", "Alive", "No", "n"]]]] 

p = codes.zip(enum0)
  #=> [["1", ["1", [["1", "Animal", "Dog", "1"],
  #                 ["1", "Animal", "Cat", "2"],
  #                 ["1", "Animal", "Bird", "3"]]]],
  #    ["1", ["2", [["2", "Place", "USA", "1"],
  #                 ["2", "Place", "Other", "2"]]]],
  #    ["a", ["3", [["3", "Color", "Red", "a"],
  #                 ["3", "Color", "Blue", "b"],
  #                 ["3", "Color", "Orange", "c"]]]],
  #    ["b", ["4", [["4", "Age", "Young", "a"],
  #                 ["4", "Age", "Middle", "b"],
  #                 ["4", "Age", "Old", "c"]]]],
  #    ["y", ["5", [["5", "Alive", "Yes", "y"],
  #                 ["5", "Alive", "No", "n"]]]]] 

l,(_,b) = enum1.next
l #=> "1" 
b #=> [["1", "Animal", "Dog", "1"], ["1", "Animal", "Cat", "2"],
  #    ["1", "Animal", "Bird", "3"]] 
enum1 = b.find
  #=> #<Enumerator: [["1", "Animal", "Dog", "1"],
  #                  ["1", "Animal", "Cat", "2"],
  #                  ["1", "Animal", "Bird", "3"]]:find> 
c = enum1.next
  #=> ["1", "Animal", "Dog", "1"] 
c.last == l
  #=> true 

所以enum1返回

d = ["1", "Animal", "Dog", "1"]
e = d[1...-1]
  #=> ["Animal", "Dog"]

因此x.zip(y)的第一个元素已映射到["Animal", "Dog"]

enum1的其他每个元素执行相同的操作后,x.zip(y)等于:

f = [["Animal", "Dog"], ["Place", "USA"], ["Color","Red"],
     ["Age",  "Middle"], ["Alive", "Yes"]]

最后的步骤是

f.to_h
  #=> {"Animal"=>"Dog", "Place"=>"USA", "Color"=>"Red",
  #    "Age"=>"Middle", "Alive"=>"Yes"}

或&lt; 2.0

Hash[f]