0

I have code like this:

combinedbooks = [[
  "0000A|0000B",
  "0000A|0000D",
  "0000B|0000D"
]]

h = Hash[combinedbooks.map {|x| [x, 1]}]

The result is:

{["0000A|0000B", "0000A|0000D", "0000B|0000D"]=>1}

What I want to have is the following:

{["0000A|0000B"]=>1, ["0000A|0000D"]=>1, ["0000B|0000D"]=>1}

I cant figure out whats the problem, I believe that there is a problem with the array declaration but im not sure about it

4
  • 2
    What you want seems to be a mapping of single-item arrays to values. Why would you want that? Commented Dec 9, 2014 at 17:59
  • Why do you have a single-element array of array? That has code smell hinting the generation of the array is wrong. Perhaps if that was fixed then the rest of the problem would be a lot easier to solve. Commented Dec 9, 2014 at 23:52
  • @theTinMan I am creating an array of arrays I think that is why. Im not sure how to just push elements of one array into another without creating an array of arrays Commented Dec 10, 2014 at 9:04
  • I added an answer to help explain the difference between a push/append vs. a concatenate/add. Commented Dec 10, 2014 at 21:27

2 Answers 2

2

If you want the keys to be single-item arrays, make them arrays:

arr = ["0000A|0000B", "0000A|0000D", "0000B|0000D"]

Hash[arr.map { |x| [[x], 1] }]

# => {["0000A|0000B"]=>1, ["0000A|0000D"]=>1, ["0000B|0000D"]=>1}

The format of your input has changed to a doubly-nested array. If that's accurate, simply use my solution, but map the first element of your array instead of the top-most array:

combinedbooks = [[ "0000A|0000B", "0000A|0000D", "0000B|0000D" ]]

Hash[combinedbooks[0].map { |x| [[x], 1] }]

# => {["0000A|0000B"]=>1, ["0000A|0000D"]=>1, ["0000B|0000D"]=>1}
Sign up to request clarification or add additional context in comments.

5 Comments

@user3540466 the output is completely identical to your expected output, except that your expected output contains a syntax error that I assumed was a typo.
@meagar The only way to get the incorrect output in the question is if combinedbooks is a nested Array. I think that is the source of confusion.
u r right there is a problem with my array definition (that I cant figure out) not with making the hash, sorry
@meagar could u explain a little bit further why a [0]. What is it exactly refering to?
@user3540466 You have an array inside an array. Something like x = [[1]]. If you access x, you're getting the value [[1]] back. If you want to get at the 1, you can't use x[0], that just returns the inner array, [1]. To get at the 1, you need x[0][0]. Similarly, you have an array inside an array: combinedbooks = [[ "0000A|0000B", "0000A|0000D", "0000B|0000D" ]]. To get at your array of values, you can't just use combinedbooks, that variable holds a one-item array. You need combinedbooks[0] to access the inner array of three items.
0

Im not sure how to just push elements of one array into another without creating an array of arrays

There are two ways people usually append to an array, and it's important to understand the difference between them. Meditate on these:

Append/push an array to another array using << results in a sub-array:

foo = []
foo << [1]
foo # => [[1]]

Concatenate/Add an array to another array using += results in the elements of the second array being appended, not the array itself:

foo = []
foo += [1]
foo # => [1]

Comments

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.