pjano1
pjano1

Reputation: 143

Comparing individual elements of an array and hash

I have the following hash and array, both with 8 elements each:

{1=>"2.06", 2=>"2.10", 4=>"2.00", 5=>"2.10", 6=>"2.20", 8=>"2.10", 9=>"2.10", 12=>"2.04"}

["2.06","2.10","2.00","2.10","2.20","2.10","2.10","2.04"]

I need to verify that each array slot matches with the corresponding hash value. For instance, array[0] needs to equal the value of hash[:1], array[1] needs to equal hash[:2], etc. What I don't want is to compare array[0] to any other hash value other than hash[:1], array[1] to any other value other than hash[:2], etc. The hash and array values are both read in the exact order pictured, so that order doesn't need to change. If any of these pairs don't match, they shall fail and iterate to the next pair.

I tried making a nested loop which iterated through the array and hash. but what it did was compare every hash value to one array slot and then iterate to the next array slot and repeat (see my current output code). This is my code so far with pseudo-code I need:

array.each_with_index do |item, index|
  hash.each do |key, value|
    puts "Key: #{key}"
    puts "Index: #{index}"
    #if item[index] == key[:value1]
    #print pass, else print fail
    #iterate to next pair
    #if item[index+1] == key[:value2]
    #repeat
    #...
    #end 
  end
end        

This is what my code currently outputs:

Key: 1
Index: 0
Key: 2
Index: 0
Key: 4
Index: 0
Key: 5
Index: 0

...

This is what I would need it to output:

Key: 1     
Index: 0
Pass
Key: 2
Index: 1
Pass
Key: 4
Index: 2
Fail
Key: 5
Index: 3
Pass

...

Upvotes: 2

Views: 77

Answers (3)

3limin4t0r
3limin4t0r

Reputation: 21120

The issue you currently face is because the second loop isn't necessary.

array.each_with_index do |item, index|
  hash.each do |key, value|
  #     ^ this is not needed

Instead access the hash by providing the key based on the index.

# setup
hash = {1=>"2.06", 2=>"2.10", 4=>"2.00", 5=>"2.10", 6=>"2.20", 8=>"2.10", 9=>"2.10", 12=>"2.04"}
array = ["2.06","2.10","2.00","2.10","2.20","2.10","2.10","2.04"]

array.each_with_index do |array_item, index|
  key = index + 1
  hash_item = hash[key]

  puts "Key: #{key}"
  puts "Index: #{index}"
  puts array_item == hash_item ? 'Pass' : 'Fail'
end

The above prints out what you expect as your output. However if you don't need the index for anything other than accessing the hash you can offset it by one by doing the following:

array.each.with_index(1) do |array_item, key|
  hash_item = hash[key]
  puts "#{key}: #{array_item == hash_item}"
end

References:

Upvotes: 0

ggorlen
ggorlen

Reputation: 56895

Doing an equality check for hash values and an array is easy:

puts hash.values == array

To do what you're trying to do, a nested loop isn't appropriate (it iterates over the entire hash for each element in array rather than comparing one to one). You can use zip to iterate over both in parallel:

array.zip(hash).each_with_index do |(av, (hk, hv)), ai|
  puts "Key: #{hk}"
  puts "Index: #{ai}"
  puts av == hv ? "pass" : "fail"
end

Output:

Key: 1
Index: 0
pass
Key: 2
Index: 1
pass
Key: 4
Index: 2
pass
Key: 5
Index: 3
pass
Key: 6
Index: 4
pass
Key: 8
Index: 5
pass
Key: 9
Index: 6
pass
Key: 12
Index: 7
pass

Try it!

Upvotes: 1

DevMasterAryan
DevMasterAryan

Reputation: 71

You can do something like this

 h={1=>"2.06", 2=>"2.10", 4=>"2.00", 5=>"2.10", 6=>"2.20", 8=>"2.10", 9=>"2.10", 12=>"2.04"}
 a = ["2.06","2.10","2.00","2.10","2.20","2.10","2.10","2.04"]
 (0).upto(a.length) do |index|
    if a[index]==h[index+1]
       puts "key: #{index+1}"
       puts "index: #{index}"
       puts "Pass"
    else
      puts "key: #{index+1}"
      puts "index: #{index}"
      puts "Failed"
    end
 end

Upvotes: 0

Related Questions