0

I have two arrays:

one = ["2cndb", "7bndb", "14accdb", "5ggdb"]
two = [["2cndb", "alive"], ["14accdb", "alive"], ["5ggdb", "not alive"]]

I want to check if each sub-array in two contains any element of one. When it does, I want to add an element "yes" to the sub-array, "no" otherwise.

My code is:

two.each do |item|
if (one.include?('item[0]'))
        item.push("yes")
    else
        item.push("no")
    end
end

and I get

two = [["2cndb", "alive", "no"], ["14accdb", "alive", "no"], ["5ggdb", "not alive", "no"]]

But "2cndb", "14accdb", "5ggdb" are present in one. Can You suggest where the problem is?

2
  • 1
    item[0] without quotes. Commented Apr 12, 2017 at 12:36
  • 'item[0]' is not item[0]. Commented Apr 12, 2017 at 12:37

1 Answer 1

2

You should use just item[0] without quotes. But you said that you want to check all values in subarrays: in this case your solution will still wrong, so possible solution is:

one = ["2cndb", "7bndb", "14accdb", "5ggdb"]
two = [["2cndb", "alive"], ["14accdb", "alive"], 
       ["5ggdb", "not alive"], ["foo", "bar"]]
two.map { |e| e + [(one & e).empty? ? 'no'  : 'yes']}
#=> [["2cndb", "alive", "yes"], ["14accdb", "alive", "yes"],
#    ["5ggdb", "not alive", "yes"], ["foo", "bar", "no"]]
Sign up to request clarification or add additional context in comments.

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.