2

I've scripted my way into a corner where I now need to compare the values of a hash to the corresponding element in an array.

I have two "lists" of the same values, sorted in different ways, and they should be identical. The interesting part is when they don't, so I need to identify those cases. So basically I need to check whether the first value of the first key-value pair in the hash is identical to the first element of the array, and likewise the second value checked towards the second element and so on for the entire set of values in the hash.

I'm sort of new to Ruby scripting, but though this should be easy enough, but alas....

4
  • 1
    Can you add an example of what you need? Commented Jul 14, 2015 at 22:25
  • is something like this? {a:2,b:3}.to_a == [[:a,2],[:b,3]] give us an example if you can Commented Jul 14, 2015 at 22:34
  • Welcome to Stack Overflow. While your description is nice, it doesn't tell us anything really. We need to see a sample of your data, show us what you've tried, and, more importantly, explain what it is you're really trying to do because, as is, this sounds like an XY problem. Commented Jul 14, 2015 at 23:40
  • Thank you, and sorry for not including examples. It was past midnight in Norway, and I’d already tried to improve my scripting skills with some home-brew ;) I see that at least one, and probably several working solutions has been put forward here, so thanks a lot! Commented Jul 15, 2015 at 5:30

2 Answers 2

3

Sounds like all you need is something simple like:

hash.keys == array

The keys should come out in the same order as they are in the Hash so this is comparing the first key of the hash with the first element of the array, the second key with the second array element, ...

You could also transliterate what you're saying into Ruby like this:

hash.each_with_index.all? { |(k, _), i| k == array[i] }

Or you could say:

hash.zip(array).all? { |(k, _), e| k == e }

The zip version is pretty much the each_with_index version with the array indexing essentially folded into the zip.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! As I was interested in the cases where the values of the Hash (sorted by keys), was not identical to the array values then hash.values != array did the trick!
In that case you could also switch the other two to use any? and != if you liked them better.
0

Technically speaking, hash is not guaranteed to ordered, so your assumption of matching the value at 'each' index of hash may not always hold true. However, for sake of answering your question, assuming h is your hash, and a is your array:

list_matches = true
h.values.each_with_index {|v, i| list_matches = list_matches && a[i] == v}

if list_matches is not equal to true than that is where the the items in the two collections don't match.

1 Comment

Technically speaking, a Hash in Ruby is guaranteed to be ordered: "Hashes enumerate their values in the order that the corresponding keys were inserted.". Unless of course you're working with a stone age version of Ruby.

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.