簡體   English   中英

如何從鍵數組和值數組創建哈希?

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

我從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']

如何將兩個數組合並為哈希?

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

如果您正在尋找將兩個相等長度的數組合並到一個哈希中,我會這樣做:

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