0

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"
}
5
  • What is @row_left, what is @row_right and what is your expected result? Commented May 25, 2016 at 9:43
  • @Stefan I'm trying to get the values of a html page of two classes row_left, row_right. I'm able to get those values from each loop. But I have a requirement where row_left value should be a key, row_right value should be a value in the form of a hash. Hope this is clear. Thankyou. Commented May 25, 2016 at 9:48
  • Could you provide some example data of both, your input and your desired output? Commented May 25, 2016 at 9:50
  • Sure, @Stefan if you consider the o/p from first loop as: Title: Author: , Price: , Description: , Seller: and let the output of second loop be: The Well-Grounded Rubyist, David A. Black, $34.99, A great book for Rubyists, Ruby Scholar Commented May 25, 2016 at 10:19
  • and the expected O/P is like: {"Title:"=>"The Well-Grounded Rubyist", "Author:"=>"David A. Black", "Price:"=>"$34.99", "Description:"=>"A great book for Rubyists", "Seller:"=>"Ruby Scholar"} Commented May 25, 2016 at 10:19

1 Answer 1

3

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)]
Sign up to request clarification or add additional context in comments.

1 Comment

I have upvoted you. But its not being shown due to lack of reputation point of 15. Anyways, Thanks once again for the answer you saved my day. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.