# Iterate over a hash with values containing a array of hashes

**URL:** <https://www.ruby-forum.com/t/iterate-over-a-hash-with-values-containing-a-array-of-hashes/179562>\
**Category:** Ruby\
**Created:** [November 28, 2009, 4:15pm UTC](https://www.ruby-forum.com/t/iterate-over-a-hash-with-values-containing-a-array-of-hashes/179562 "2009-11-28T16:15:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![m4lloc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/87869e/32.png) [@m4lloc](https://www.ruby-forum.com/u/m4lloc)\
**Post date:** [November 28, 2009, 4:15pm UTC](https://www.ruby-forum.com/t/iterate-over-a-hash-with-values-containing-a-array-of-hashes/179562/1 "2009-11-28T16:15:55Z")

</div>

Hi

I need iterate over a hash with values containing a array of hashes for  
build a string with comma values

I tried tranform this

{“F15”=\> [  
{“first”=\>“01” , “last”=\>"" , “data”=\>“ATX”, “Xcode”=\>“F15”,  
“type”=\>“G\_TRD”},  
{“first”=\>“Aero”, “last”=\>“Z”, “data”=\>“VTA”, “Xcode”=\>“F15”,  
“type”=\>“H\_TGFE”},  
{“first”=\>“digg”, “last”=\>"" , “data”=\>“GFC”, “Xcode”=\>“F15”,  
“type”=\>“T\_1000”}],  
“F17”=\> [  
{“first”=\>“X01”, “last”=\>“Sx” , “data”=\>“ATT”, “Xcode”=\>“F17”,  
“type”=\>“H\_TGFE”},  
{“first”=\>“DEF”, “last”=\>“Z” , “data”=\>“VTA”, “Xcode”=\>“F17”,  
“type”=\>“G\_TRD”},  
{“first”=\>“EDF”, “last”=\>“FR” , “data”=\>“CFC”, “Xcode”=\>“F17”,  
“type”=\>“T\_900”}]}

into a string like this:

“F15;first;01;last;;data;ATX;Xcode;F15;type;G\_TRD”  
“F15;first;Aero;last;Z;data;VTA;Xcode;F15;type;H\_TGFE”  
“F15;first;digg;last;;data;GFC;Xcode;F15;type;T\_1000”  
“F17;first;X01;last;Sx;data;ATT;Xcode;F17;type;H\_TGFE”  
“F17;first;DEF;last;Z;data;VTA;Xcode;F17;type;G\_TRD”  
“F17;first;EDF;last;FR;data;CFC;Xcode;F17;type;T\_900”

I believe that this task isn’t complex, but for me is difficult to do  
now

Everyone can help me with this?

thanks so much

---

<div class="post-metadata">

**Author:** ![m4lloc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/87869e/32.png) [@m4lloc](https://www.ruby-forum.com/u/m4lloc)\
**Post date:** [November 28, 2009, 5:22pm UTC](https://www.ruby-forum.com/t/iterate-over-a-hash-with-values-containing-a-array-of-hashes/179562/2 "2009-11-28T17:22:33Z")

</div>

On Sat, Nov 28, 2009 at 10:15 AM, Bruno M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> I need iterate over a hash with values containing a array of hashes for  
> build a string with comma values
> 
> I tried tranform this  
> …  
> into a string like this:  
> …  
> I believe that this task isn’t complex, but for me is difficult to do  
> now

How far were you able to get before getting stuck? The basic way to  
iterate over a Hash is each:

irb(main):001:0\> hash = {“A” =\> 1, “B” =\> 2}  
=\> {“A”=\>1, “B”=\>2}  
irb(main):002:0\> hash.each{|key, value|  
irb(main):003:1\* p value  
irb(main):004:1\> }  
1  
2  
=\> {“A”=\>1, “B”=\>2}

When the values stored in the Hash are Arrays, you can use each within  
each:

irb(main):005:0\> hash = {“A” =\> [1,2,3], “B” =\>[4,5,6]}  
=\> {“A”=\>[1, 2, 3], “B”=\>[4, 5, 6]}  
irb(main):006:0\> hash.each{|key, array|  
irb(main):007:1\* p key  
irb(main):008:1\> array.each{|element|  
irb(main):009:2\* p element  
irb(main):010:2\> }  
irb(main):011:1\> }  
“A”  
1  
2  
3  
“B”  
4  
5  
6  
=\> {“A”=\>[1, 2, 3], “B”=\>[4, 5, 6]}

Does that help get you moving in the right direction?

---

<div class="post-metadata">

**Author:** ![m4lloc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/87869e/32.png) [@m4lloc](https://www.ruby-forum.com/u/m4lloc)\
**Post date:** [November 28, 2009, 6:20pm UTC](https://www.ruby-forum.com/t/iterate-over-a-hash-with-values-containing-a-array-of-hashes/179562/3 "2009-11-28T18:20:30Z")

</div>

Bruno M. wrote:

> Hi
> 
> I need iterate over a hash with values containing a array of hashes for  
> build a string with comma values
> 
> (…)
> 
> Everyone can help me with this?
> 
> thanks so much

###========================

input = {“F15”=\> [  
{“first”=\>“01”, “last”=\>"" , “data”=\>“ATX”,  
“Xcode”=\>“F15”, “type”=\>“G\_TRD”},  
{“first”=\>“Aero”, “last”=\>“Z”, “data”=\>“VTA”,  
“Xcode”=\>“F15”, “type”=\>“H\_TGFE”},  
{“first”=\>“digg”, “last”=\>"" , “data”=\>“GFC”,  
“Xcode”=\>“F15”, “type”=\>“T\_1000”}],  
“F17”=\> [  
{“first”=\>“X01”, “last”=\>“Sx”, “data”=\>“ATT”,  
“Xcode”=\>“F17”, “type”=\>“H\_TGFE”},  
{“first”=\>“DEF”, “last”=\>“Z”, “data”=\>“VTA”,  
“Xcode”=\>“F17”, “type”=\>“G\_TRD”},  
{“first”=\>“EDF”, “last”=\>“FR” , “data”=\>“CFC”,  
“Xcode”=\>“F17”, “type”=\>“T\_900”}]}

chk\_answer = [“F15;first;01;last;;data;ATX;Xcode;F15;type;G\_TRD”,  
“F15;first;Aero;last;Z;data;VTA;Xcode;F15;type;H\_TGFE”,  
“F15;first;digg;last;;data;GFC;Xcode;F15;type;T\_1000”,  
“F17;first;X01;last;Sx;data;ATT;Xcode;F17;type;H\_TGFE”,  
“F17;first;DEF;last;Z;data;VTA;Xcode;F17;type;G\_TRD”,  
“F17;first;EDF;last;FR;data;CFC;Xcode;F17;type;T\_900” ]

#~~~  
res = [] # results

input.sort.each do |k1,a1|  
a1.each do |h|  
out = ‘’ \<\< k1  
[‘first’, ‘last’, ‘data’, ‘Xcode’, ‘type’].each do |k2|  
out \<\< sprintf(’;%s;%s’, k2, h[k2]) # append to string  
end  
res \<\< out # push line into result array  
end  
end  
#~~~

puts ‘\*\*\* C o r r e c t \*\*\*’ if res == chk\_answer  
puts res

###========================

# 🙂

daz

---

<div class="post-metadata">

**Author:** ![m4lloc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/87869e/32.png) [@m4lloc](https://www.ruby-forum.com/u/m4lloc)\
**Post date:** [November 28, 2009, 11:45pm UTC](https://www.ruby-forum.com/t/iterate-over-a-hash-with-values-containing-a-array-of-hashes/179562/4 "2009-11-28T23:45:17Z")

</div>

Thanks guys, you rocks!

The snippets that you showed save my time!

I can iterate over a simple hash or array, but when is necessary work if  
a more complex structure, like a array of hashes or a hash of array of  
hashes I  
feel paralyzed.I don’t know how I should use blocks in the right way or  
efficiently.

For me is really difficult to understand this.

How can I improve my practice with this kind of task?

Thanks again!

daz wrote:

> Bruno M. wrote:
> 
> > Hi
> > 
> > I need iterate over a hash with values containing a array of hashes for  
> > build a string with comma values
> > 
> > (…)
> > 
> > Everyone can help me with this?
> > 
> > thanks so much
> 
> ###========================
> 
> input = {“F15”=\> [  
> {“first”=\>“01”, “last”=\>"" , “data”=\>“ATX”,  
> “Xcode”=\>“F15”, “type”=\>“G\_TRD”},  
> {“first”=\>“Aero”, “last”=\>“Z”, “data”=\>“VTA”,  
> “Xcode”=\>“F15”, “type”=\>“H\_TGFE”},  
> {“first”=\>“digg”, “last”=\>"" , “data”=\>“GFC”,  
> “Xcode”=\>“F15”, “type”=\>“T\_1000”}],  
> “F17”=\> [  
> {“first”=\>“X01”, “last”=\>“Sx”, “data”=\>“ATT”,  
> “Xcode”=\>“F17”, “type”=\>“H\_TGFE”},  
> {“first”=\>“DEF”, “last”=\>“Z”, “data”=\>“VTA”,  
> “Xcode”=\>“F17”, “type”=\>“G\_TRD”},  
> {“first”=\>“EDF”, “last”=\>“FR” , “data”=\>“CFC”,  
> “Xcode”=\>“F17”, “type”=\>“T\_900”}]}
> 
> chk\_answer = [“F15;first;01;last;;data;ATX;Xcode;F15;type;G\_TRD”,  
> “F15;first;Aero;last;Z;data;VTA;Xcode;F15;type;H\_TGFE”,  
> “F15;first;digg;last;;data;GFC;Xcode;F15;type;T\_1000”,  
> “F17;first;X01;last;Sx;data;ATT;Xcode;F17;type;H\_TGFE”,  
> “F17;first;DEF;last;Z;data;VTA;Xcode;F17;type;G\_TRD”,  
> “F17;first;EDF;last;FR;data;CFC;Xcode;F17;type;T\_900” ]
> 
> #~~~  
> res = [] # results
> 
> input.sort.each do |k1,a1|  
> a1.each do |h|  
> out = ‘’ \<\< k1  
> [‘first’, ‘last’, ‘data’, ‘Xcode’, ‘type’].each do |k2|  
> out \<\< sprintf(’;%s;%s’, k2, h[k2]) # append to string  
> end  
> res \<\< out # push line into result array  
> end  
> end  
> #~~~
> 
> puts ‘\*\*\* C o r r e c t \*\*\*’ if res == chk\_answer  
> puts res
> 
> ###========================
> 
> # 🙂
> 
> daz
