From: Bruno Moura Date: 2009-11-29T07:45:18+09:00 Subject: Re: iterate over a hash with values containing a array of hashes 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 Moura 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 -- Posted via http://www.ruby-forum.com/.