Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 在一个数组中将多个数组转换为单独的哈希_Ruby_Arrays_Merge_Hash - Fatal编程技术网

Ruby 在一个数组中将多个数组转换为单独的哈希

Ruby 在一个数组中将多个数组转换为单独的哈希,ruby,arrays,merge,hash,Ruby,Arrays,Merge,Hash,我需要使用以下数据来构造哈希数组。数组中的第一个元素应为: { salutation: 'Mr.' first_name: 'John', last_name: 'Dillinger', address: '33 Foolish Lane, Boston MA 02210' } 给出的数据如下。我真的很难想出如何做到这一点。一些帮助将不胜感激,因为我现在已经智穷了 salutations = [ 'Mr.', 'Mrs.', 'Mr.',

我需要使用以下数据来构造哈希数组。数组中的第一个元素应为:

 {
   salutation: 'Mr.'
   first_name: 'John',
   last_name: 'Dillinger',
   address: '33 Foolish Lane, Boston MA 02210'
 }
给出的数据如下。我真的很难想出如何做到这一点。一些帮助将不胜感激,因为我现在已经智穷了

 salutations = [
   'Mr.',
   'Mrs.',
   'Mr.',
   'Dr.',
   'Ms.'
 ]

 first_names = [
   'John',
   'Jane',
   'Sam',
   'Louise',
   'Kyle'
 ]

 last_names = [
   'Dillinger',
   'Cook',
   'Livingston',
   'Levinger',
   'Merlotte'
 ]

 addresses = [
   '33 Foolish Lane, Boston MA 02210',
   '45 Cottage Way, Dartmouth, MA 02342',
   "54 Sally's Court, Bridgewater, MA 02324",
   '4534 Broadway, Boston, MA 02110',
   '4231 Cynthia Drive, Raynham, MA 02767'
 ]
我能想出的唯一解决办法不起作用。知道为什么吗

array_of_hashes = []
array_index = 0

 def array_to_hash (salutations, first_names, last_names, addresses)
   while array_index <= 5
     hash = {}
     hash[salutation] = salutations(array_index)
     hash[first_name] = first_names(array_index)
     hash[last_name] = last_names(array_index)
     hash[address] = addresses(array_index)
     array_of_hashes << hash
     array_index += 1
  end
end

array_to_hash(salutations,first_names,last_names,addresses)
array\u of_hash=[]
数组_索引=0
def数组到散列(称呼、名字、姓氏、地址)
while数组_索引
或者,如果您不确定每个数组中是否只有五个项目,请将
5.times
替换为例如
saltations.length.times

[salutations, first_names, last_names, addresses].transpose
.map{|a| Hash[%i[salutation first_name last_name address].zip(a)]}

或者如果您不确定每个数组中是否只有五个项目,将
5.times
替换为例如
saltations.length.times

您必须传递
array\u index
作为参数,因为您无法访问方法外部的变量

您必须传递
array\u index
作为参数传递变量位于方法的外部。

称呼.zip(名字、姓氏、地址)。映射…
?:)@你好。这同样有效,但我尊重这里的对称性。谢谢@sawa,感谢你的帮助。我自己能想出的唯一解决办法现在也列在问题中。你知道为什么这样不行吗?这是因为没有像
称呼(array\u index)
这样的方法。如果您想在索引
array\u index
处获取
saluations
的元素,那么您需要像Zippie的回答中那样执行
salations[array\u index]
。实际上我仍然收到一个错误:“
array\u to\u hash”:未定义的局部变量或main:Object(namererror)”的方法。关于这是什么原因有什么见解吗?
saltations.zip(名字、姓氏、地址)。map…
?:)@你好。这同样有效,但我尊重这里的对称性。谢谢@sawa,感谢你的帮助。我自己能想出的唯一解决办法现在也列在问题中。你知道为什么这样不行吗?这是因为没有像
称呼(array\u index)
这样的方法。如果您想在索引
array\u index
处获取
saluations
的元素,那么您需要像Zippie的回答中那样执行
salations[array\u index]
。实际上我仍然收到一个错误:“
array\u to\u hash”:未定义的局部变量或main:Object(namererror)”的方法。你知道为什么吗?谢谢Zippie。在本例中,x代表数组中的索引称呼、名字、姓氏和地址吗?是的,x代表索引Hanks Zippie。在这种情况下,x代表数组中的索引称呼、名字、姓氏和地址吗?是的,x是索引
[salutations, first_names, last_names, addresses].transpose
.map{|a| Hash[%i[salutation first_name last_name address].zip(a)]}
[salutations, first_names, last_names, addresses].transpose
.map{|a| Hash[%i[salutation first_name last_name address].zip(a)]}
keys = [:salutations, :first_names, :last_names, :addresses]
data = [ salutations,  first_names,  last_names,  addresses].transpose

data.map!{|person|  Hash[keys.zip(person)]}