On Tue, Feb 15, 2011 at 10:51 AM, Sem P. [email protected]
wrote:
If I have the following:
[[148,“Uneducated”],[135,“Highschool”],[272,"Some
college"],[141,“University”],[143,“Post-grad”],[144,“PhD”],[141,“other”],[8,“Post-doc”]]
is there an function to invert/swap the numbers with strings?
irb(main):001:0> [[148,“Uneducated”],[135,“Highschool”],[272,“Some
irb(main):002:2”
college"],[141,“University”],[143,“Post-grad”],[144,“PhD”],[141,“other”],[8,“Post-doc”]].each
{|a| a.reverse!}
=> [[“Uneducated”, 148], [“Highschool”, 135], [“Some\ncollege”, 272],
[“University”, 141], [“Post-grad”, 143], [“PhD”, 144], [“other”, 141],
[“Post-doc”, 8]]
Btw, for your original problem there is an elegant solution which has
not been shown yet:
irb(main):003:0> [{0=>“BSD”, 1=>105, “Operating System”=>“BSD”,
"count()"=>105},
irb(main):004:1 {0=>“SUSE”, 1=>99, “Operating System”=>“SUSE”,
"count()"=>99},
irb(main):005:1 {0=>“Ubuntu”, 1=>97, “Operating System”=>“Ubuntu”,
"count()"=>97},
irb(main):006:1 {0=>“Unix”, 1=>190, “Operating System”=>“Unix”,
"count()"=>190},
irb(main):007:1 {0=>“Windows XP”, 1=>92, “Operating System”=>“Windows
XP”,
irb(main):008:2* “count()"=>92}].each {|h| h.delete_if {|k,| Integer
=== k}}
=> [{“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}]
Although you have to consider that this works only if you do not have
numeric keys that you want to keep.
Cheers
robert