# Hash sanitization from query - newbie

**URL:** <https://www.ruby-forum.com/t/hash-sanitization-from-query-newbie/202909>\
**Category:** Ruby\
**Created:** [February 8, 2011, 8:27pm UTC](https://www.ruby-forum.com/t/hash-sanitization-from-query-newbie/202909 "2011-02-08T20:27:27Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [February 8, 2011, 8:27pm UTC](https://www.ruby-forum.com/t/hash-sanitization-from-query-newbie/202909/1 "2011-02-08T20:27:27Z")

</div>

I need to clean up a hash

[{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}]

You’ll notice the first part is an array key value pair and the second  
part of each hash is a real key value pair.

How would I go about removing all the 0=\> and 1=\> references from the  
above hash?

I’ve seen people use inject and map but are a bit lost with regards to  
those functions.

---

<div class="post-metadata">

**Author:** ![sempti](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/96bed5/32.png) [@sempti](https://www.ruby-forum.com/u/sempti)\
**Post date:** [February 8, 2011, 8:57pm UTC](https://www.ruby-forum.com/t/hash-sanitization-from-query-newbie/202909/2 "2011-02-08T20:57:45Z")

</div>

On Tue, Feb 8, 2011 at 1:27 PM, Sem P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> part of each hash is a real key value pair.
> 
> How would I go about removing all the 0=\> and 1=\> references from the  
> above hash?

# [http://ruby-doc.org/core/classes/Hash.html#M000747](http://ruby-doc.org/core/classes/Hash.html#M000747)

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}  
]

oses.each do |os|  
os.delete 0  
os.delete 1  
end

require ‘pp’  
pp oses

# \>\> [{“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}]

---

<div class="post-metadata">

**Author:** ![sempti](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/96bed5/32.png) [@sempti](https://www.ruby-forum.com/u/sempti)\
**Post date:** [February 9, 2011, 8:09am UTC](https://www.ruby-forum.com/t/hash-sanitization-from-query-newbie/202909/3 "2011-02-09T08:09:53Z")

</div>

On Wed, Feb 9, 2011 at 3:27 AM, Sem P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> I’ve seen people use inject and map but are a bit lost with regards to  
> those functions.

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

---

<div class="post-metadata">

**Author:** ![sempti](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/96bed5/32.png) [@sempti](https://www.ruby-forum.com/u/sempti)\
**Post date:** [February 15, 2011, 10:51am UTC](https://www.ruby-forum.com/t/hash-sanitization-from-query-newbie/202909/4 "2011-02-15T10:51:16Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![sempti](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/96bed5/32.png) [@sempti](https://www.ruby-forum.com/u/sempti)\
**Post date:** [February 9, 2011, 8:18am UTC](https://www.ruby-forum.com/t/hash-sanitization-from-query-newbie/202909/5 "2011-02-09T08:18:55Z")

</div>

Thanks for the comparisons.

---

<div class="post-metadata">

**Author:** ![sempti](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/96bed5/32.png) [@sempti](https://www.ruby-forum.com/u/sempti)\
**Post date:** [February 15, 2011, 11:04am UTC](https://www.ruby-forum.com/t/hash-sanitization-from-query-newbie/202909/6 "2011-02-15T11:04:43Z")

</div>

Maybe I misunderstand what you are trying to do here, but what about  
something like:

studies = [[148,“Uneducated”],[135,“Highschool”],[272,“Some  
college”],[141,“University”],[143,“Post-grad”],[144,“PhD”],[141,“other”],[8,“Post-doc”]]

studies.map(&:reverse)  
=\> [[“Uneducated”, 148], [“Highschool”, 135], [“Some college”, 272],  
[“University”, 141], [“Post-grad”, 143], [“PhD”, 144], [“other”, 141],  
[“Post-doc”, 8]]

–  
Estanislau Trepat

> **[etrepat.com | Estanislau Trepat](https://etrepat.com/)**
>
> Estanislau Trepat's Home Page

[http://twitter.com/etrepat](http://twitter.com/etrepat)

2011/2/15 Sem P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![sempti](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/96bed5/32.png) [@sempti](https://www.ruby-forum.com/u/sempti)\
**Post date:** [February 15, 2011, 11:07am UTC](https://www.ruby-forum.com/t/hash-sanitization-from-query-newbie/202909/7 "2011-02-15T11:07:13Z")

</div>

On Tue, Feb 15, 2011 at 10:51 AM, Sem P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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?

Take a look at map or map! (if you want to modify the object in place):

irb(main):005:0\> a = [[148,“Uneducated”],[135,“Highschool”],[272,“Some  
college”],[141,“University”],[143,“Post-grad”],[144,“PhD”],[141,“other”],[8,“Post-doc”]]  
=\> [[148, “Uneducated”], [135, “Highschool”], [272, “Some college”],  
[141, “University”], [143, “Post-grad”], [144, “PhD”], [141, “other”],  
[8, “Post-doc”]]  
irb(main):006:0\> a.map {|(num,string)| [string,num]}  
=\> [[“Uneducated”, 148], [“Highschool”, 135], [“Some college”, 272],  
[“University”, 141], [“Post-grad”, 143], [“PhD”, 144], [“other”, 141],  
[“Post-doc”, 8]]

Jesus.

---

<div class="post-metadata">

**Author:** ![sempti](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/96bed5/32.png) [@sempti](https://www.ruby-forum.com/u/sempti)\
**Post date:** [February 15, 2011, 11:05am UTC](https://www.ruby-forum.com/t/hash-sanitization-from-query-newbie/202909/8 "2011-02-15T11:05:58Z")

</div>

On Tue, Feb 15, 2011 at 10:51 AM, Sem P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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
