oses = [
{0=>"BSD", 1=>105, "Operating System"=>"BSD", "count(*)"=>105},
{0=>"SUSE", 1=>99, "Operating System"=>"SUSE", "count(*)"=>99},
{0=>"Ubuntu", 1=>97, "Operating System"=>"Ubuntu", "count(*)"=>97},
{0=>"Unix", 1=>190, "Operating System"=>"Unix", "count(*)"=>190},
{0=>"Windows XP", 1=>92, "Operating System"=>"Windows XP","count(*)"=>92}
]
# map version
oses.map{|h| h.delete_if{|k,_| k==0 or k==1}}
#=> [{"Operating System"=>"BSD", "count(*)"=>105}, {"Operating
System"=>"SUSE", "count(*)"=>99}, {"Operating System"=>"Ubuntu",
"count(*)"=>97}, {"Operating System"=>"Unix", "count(*)"=>190},
{"Operating System"=>"Windows XP", "count(*)"=>92}]
# inject version
oses.inject(){|a,h| a << h.delete_if{|k,_| k==0 or k==1} }
#=> [{"Operating System"=>"BSD", "count(*)"=>105}, {"Operating
System"=>"SUSE", "count(*)"=>99}, {"Operating System"=>"Ubuntu",
"count(*)"=>97}, {"Operating System"=>"Unix", "count(*)"=>190},
{"Operating System"=>"Windows XP", "count(*)"=>92}]
but note, you have repeating keys wc could be just be fields themselves, eg
oses.map{|h| h.delete_if{|k,_| k==0 or k==1}; h.values}
#=> [["BSD", 105], ["SUSE", 99], ["Ubuntu", 97], ["Unix", 190],
["Windows XP", 92]]
or
Hash[ *oses.map{|h| h.delete_if{|k,_| k==0 or k==1}; h.values}.flatten ]
#=> {"BSD"=>105, "SUSE"=>99, "Ubuntu"=>97, "Unix"=>190, "Windows XP"=>92}
best regards -botp
···
On Wed, Feb 9, 2011 at 3:27 AM, Sem Ptiri <rubyforum@chthonic.co.za> wrote:
I've seen people use inject and map but are a bit lost with regards to
those functions.