Display two hashes

Hi all,

I have been trying a script to count and display messages in two Imap
accounts. I’m iterating each account and I create two hashes like this:

Hash1 {name, count}
Hash2 {name, count}

I’m trying to figure out a way to display both hashes with their
differences
(folders that exist or dont exist in both accounts) like that :

Hash1{name, count} , Hash2{name, count} (folders exist in both)
Hash1{name, count} , Hash2{} (folder exist only in1)
Hash1{}, Hash2{name, count} (folder exist only in 2)

what would be the way to solve this problem?

thanks in advance

I do not like to give a solution so I have a tip for you.

What you are looking for are called intersection and distinct, you
always have to learn a bit of math. Please let us know if you still
cannot solve the issue. Understanding of such things will greatly
improve the way you think and code. Just trust me and do not give up!

a=(0…10).to_a
d=(3…6).to_a
b=a-d+(9…13).to_a
h1=a.each_with_object({}) {|k,o| o[k]=k}
h2=b.each_with_object({}) {|k,o| o[k]=k}

h1.merge(h2).each_key {|k|
puts “h1(%2s) | h2(%s)” %
[h1[k] ? k.to_s : “”,h2[k] ? k.to_s : “”]
}

ruby t.rb
h1( 0) | h2(0)
h1( 1) | h2(1)
h1( 2) | h2(2)
h1( 3) | h2()
h1( 4) | h2()
h1( 5) | h2()
h1( 6) | h2()
h1( 7) | h2(7)
h1( 8) | h2(8)
h1( 9) | h2(9)
h1(10) | h2(10)
h1( ) | h2(11)
h1( ) | h2(12)
h1( ) | h2(13)

Thanks , you’ve been very helpfull anyway , I working on it now and I
think
I got it!!

thanks again

this is too advanced for me … I’m completely new to ruby, I can
speculate what you’re doing but I cant say that I’m able to follow…

thanks anyway!

Sorry, i am perhaps a little stress…

Regis d’Aubarede wrote in post #1174443:

a=(0…10).to_a
d=(3…6).to_a
b=a-d+(9…13).to_a

create a lists (Array) of numbers :
a will be 1,2,3…10
b will be 1,2,3,6,7,8,9,9,10,10,11…,13

h1=a.each_with_object({}) {|k,o| o[k]=k}
h2=b.each_with_object({}) {|k,o| o[k]=k}

create Hash with this list , so
h1={1=>1, 2=>2, … 10=>10}
h2={1=>1,2=>2,7=>7 …, 11=>11}

h1.merge(h2)
create a Hash which is the ‘sum’ of h1 and h2 each entry
in h1, and then h2, are added to a new Hash.

So the result have all the keys of h1 AND h2

h1.merge(h2).each_key {|k|
iterate on each entry of the merge

puts “h1(%2s) | h2(%s)” %
[h1[k] ? k.to_s : “”,h2[k] ? k.to_s : “”]

format a string :
“h1(%2s) | h2(%s)” % [“a”,“b”]
===> %2s will be replaced by “a”
===> %s will be replaced by “b”

h1[k] ? k.to_s : “”

if the entry k exist in Hash h1, return k , converted in string
else, return empty string

Very simple, no?
:slight_smile:

Thanks , your approach helped me to find ways to print (puts) the
data I’m interested . You and Taras have been very helpfull

thanks a lot!!

h1.merge(h2).each_key {|k|
iterate on each entry of the merge

puts “h1(%2s) | h2(%s)” %
[h1[k] ? k.to_s : “”,h2[k] ? k.to_s : “”]

format a string :
“h1(%2s) | h2(%s)” % [“a”,“b”]
===> %2s will be replaced by “a”
===> %s will be replaced by “b”

h1[k] ? k.to_s : “”

if the entry k exist in Hash h1, return k , converted in string
else, return empty string

Very simple, no?
:slight_smile:

I tried to modify your suggestion to print the value also (besides the
key)
like that :

h1.merge(h2).each_key {|k,v|
puts “h1(%2s) | h2(%s)” %
h1[k] ? k.to_s +v : “”,h2[k] ? k.to_s +v : “”]
or
h1[k] ? (k.to_s +v ): “”,h2[k] ? (k.to_s +v) : “”]
}

but doesn’t seem to work

in irb hash I can see the values returned (together with the keys)
but how can I print them ?

I also tried h1[k,v] ? k.to_s +v : “”,h2[k,v] ? k.to_s +v : “”]

probably something more to do ?

George Papamichelakis wrote in post #1174516:

    h1[k] ? k.to_s +v : "",h2[k] ? k.to_s +v  : ""]

but doesn’t seem to work

in irb hash I can see the values returned (together with the keys)
but how can I print them ?

v.inspect give a string representing what you see with irb
inspect in api doc:
“Returns a string containing a human-readable representation of obj.”

so :
[h1[k] ? “#{k}#{h1[k].inspect}” : “”,
h2[k] ? #{k}#{h2[k].inspect}" : “”]

bump !!!

as simple as it can be! ( I have a long way to go to learn ruby)
thanks

Create two hashes for testing.

h1 = {1=>10,2=>11,3=>12}
h2 = {0=>0,1=>1,2=>2}

Get the keys of both hashes and

compute which occurs in both and

which occur only in one array.

For example, [1,2]&[2,3]=[2]

and [1,2]-[2,3]=[1]

both = h1.keys & h2.keys
only_h1 = h1.keys - h2.keys
only_h2 = h2.keys - h1.keys

Print the result, each key and the

corresponding value in the hash.

puts “both”
both.each{|x|puts “#{x} => #{h1[x]} and #{h2[x]}”}

puts “only h1”
only_h1.each{|x|puts “#{x} => #{h1[x]}”}

puts “only h2”
only_h2.each{|x|puts “#{x} => #{h2[x]}”}