Atchyut Nagabhairava
Atchyut Nagabhairava

Reputation: 1413

How to create a hash from keys array and values array?

I have two arrays extracted from a HTML page:

@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"
}

Upvotes: 0

Views: 173

Answers (1)

unflores
unflores

Reputation: 1810

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)]

Upvotes: 3

Related Questions