简体   繁体   English

如何从键数组和值数组创建哈希?

[英]How to create a hash from keys array and values array?

I have two arrays extracted from a HTML page: 我从HTML页面提取了两个数组:

@row_left = ['Title:', 'Author:', 'Price:', 'Description:', 'Seller:']    
@row_right = ['The Well-Grounded Rubyist', 'David A. Black', '$34.99', 'A great book for Rubyists', 'Ruby Scholar']

How can I combine both arrays into a hash? 如何将两个数组合并为哈希?

{
  "Title:" => "The Well-Grounded Rubyist",
  "Author:" => "David A. Black",
  "Price:" => "$34.99",
  "Description:" => "A great book for Rubyists",
  "Seller:" => "Ruby Scholar"
}

If you are looking for merging two equal length arrays into a hash, I would do this: 如果您正在寻找将两个相等长度的数组合并到一个哈希中,我会这样做:

a = [1,3,5]; b=[2,4,6]
Hash[a.zip(b)]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM