replace values in hash

Discussion in 'Ruby' started by Jason Lillywhite, Nov 5, 2008.

  1. how would I iterate over a hash, such as

    x = { 'a' => "hi", 'b' => nil, 'c' => "do"}

    and replace nil values with 'foo' then return the hash again?
    --
    Posted via http://www.ruby-forum.com/.
     
    Jason Lillywhite, Nov 5, 2008
    #1
    1. Advertising

  2. Jason Lillywhite

    Trans Guest

    On Nov 5, 1:53=A0am, Jason Lillywhite <>
    wrote:
    > how would I iterate over a hash, such as
    >
    > x =3D { 'a' =3D> "hi", 'b' =3D> nil, 'c' =3D> "do"}


    x.each{ |k,v| x[k] =3D 'foo' unless v }

    However, it is not always sane to alter something while you are
    iterating over it. So,

    h =3D {}
    x.each{ |k,v| h[k] =3D v.nil? ? v : 'foo' }
    x.replace(h)

    Or use Facets Enumerable#mash (alias #graph)

    require 'facets/enumerable/mash'

    x =3D x.mash{ |k,v| [k, v.nil? ? v : 'foo'] }

    http://facets.rubyforge.org/doc/api/core/classes/Enumerable.html#M000429

    7.
     
    Trans, Nov 5, 2008
    #2
    1. Advertising

  3. Thank you!

    However, I am getting undefined method 'mash' even though I do require
    'facets/enumerable/mash'

    am I missing something?
    --
    Posted via http://www.ruby-forum.com/.
     
    Jason Lillywhite, Nov 5, 2008
    #3
  4. =20

    > -----Original Message-----
    > From: [mailto:]=20
    > Sent: Wednesday, November 05, 2008 12:24 PM
    > To: ruby-talk ML
    > Subject: replace values in hash
    >=20
    > how would I iterate over a hash, such as
    >=20
    > x =3D { 'a' =3D> "hi", 'b' =3D> nil, 'c' =3D> "do"}



    h.keys.each {|d| h[d] =3D "foo" if h[d] =3D=3D nil}
     
    Sarcar, Shourya C (GE Healthcare), Nov 5, 2008
    #4
  5. Jason Lillywhite

    Trans Guest

    On Nov 5, 2:54=A0am, Jason Lillywhite <>
    wrote:
    > Thank you!
    >
    > However, I am getting undefined method 'mash' even though I do require
    > 'facets/enumerable/mash'
    >
    > am I missing something?


    Don't think so. It's working fine for me.

    What version of Ruby and Facets and what platform are you running?

    T.
     
    Trans, Nov 5, 2008
    #5
  6. Jason Lillywhite

    Brian Adkins Guest

    Jason Lillywhite <> writes:

    > how would I iterate over a hash, such as
    >
    > x = { 'a' => "hi", 'b' => nil, 'c' => "do"}
    >
    > and replace nil values with 'foo' then return the hash again?


    ~/temp$ cat -b temp.rb
    1 require 'pp'

    2 class Hash
    3 def replace_value old, new
    4 self.inject({}) do |result,pair|
    5 k, v = pair[0], pair[1]
    6 result[k] = (v == old) ? new : v
    7 result
    8 end
    9 end
    10 end

    11 x = { 'a' => "hi", 'b' => nil, 'c' => "do", :d => nil }
    12 pp x
    13 y = x.replace_value(nil, 'foo')
    14 pp y
    ~/temp$ ruby temp.rb
    {"a"=>"hi", "b"=>nil, :d=>nil, "c"=>"do"}
    {"a"=>"hi", "b"=>"foo", "c"=>"do", :d=>"foo"}

    --
    Brian Adkins
    http://www.lojic.com/
    http://lojic.com/blog/
     
    Brian Adkins, Nov 5, 2008
    #6
  7. Jason Lillywhite

    Trans Guest

    On Nov 5, 10:31=A0am, Trans <> wrote:
    > On Nov 5, 2:54=A0am, Jason Lillywhite <>
    > wrote:
    >
    > > Thank you!

    >
    > > However, I am getting undefined method 'mash' even though I do require
    > > 'facets/enumerable/mash'

    >
    > > am I missing something?

    >
    > Don't think so. It's working fine for me.
    >
    > What version of Ruby and Facets and what platform are you running?


    For anyone who is interested, here's the definition (and some side
    notes about how it evolved).

    def mash(&yld)
    if yld
    inject({}) do |h, *kv| # Used to be inject({}) do |h,kv|
    r =3D *yld[*kv] # The *-op works different from to_a on
    single element hash!!!
    nk, nv =3D *r # Used to be nk, nv =3D
    *yld[*kv].to_a.flatten
    h[nk] =3D nv
    h
    end
    else
    Enumerator.new(self,:mash) # Used to be Hash[*self.to_a]
    end
    end

    T.
     
    Trans, Nov 5, 2008
    #7
  8. [Note: parts of this message were removed to make it a legal post.]

    Not bad. Let's tighten it up a little:

    class Hash
    def replace_value(old, new)
    self.inject({}) do |result, (key, existing_value)|
    result[key] = (value == old) ? new : existing_value
    result
    end
    end
    end

    This isn't bad, but you wouldn't want to do this for large Hashes since you
    end up copying the original.

    James

    On Wed, Nov 5, 2008 at 12:38 PM, Brian Adkins <> wrote:

    > Jason Lillywhite <> writes:
    >
    > > how would I iterate over a hash, such as
    > >
    > > x = { 'a' => "hi", 'b' => nil, 'c' => "do"}
    > >
    > > and replace nil values with 'foo' then return the hash again?

    >
    > ~/temp$ cat -b temp.rb
    > 1 require 'pp'
    >
    > 2 class Hash
    > 3 def replace_value old, new
    > 4 self.inject({}) do |result,pair|
    > 5 k, v = pair[0], pair[1]
    > 6 result[k] = (v == old) ? new : v
    > 7 result
    > 8 end
    > 9 end
    > 10 end
    >
    > 11 x = { 'a' => "hi", 'b' => nil, 'c' => "do", :d => nil }
    > 12 pp x
    > 13 y = x.replace_value(nil, 'foo')
    > 14 pp y
    > ~/temp$ ruby temp.rb
    > {"a"=>"hi", "b"=>nil, :d=>nil, "c"=>"do"}
    > {"a"=>"hi", "b"=>"foo", "c"=>"do", :d=>"foo"}
    >
    > --
    > Brian Adkins
    > http://www.lojic.com/
    > http://lojic.com/blog/
    >
    >
     
    James Herdman, Nov 10, 2008
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. rp
    Replies:
    1
    Views:
    581
    red floyd
    Nov 10, 2011
  2. Alex Fenton

    Hash#values and Hash#keys order

    Alex Fenton, Apr 7, 2006, in forum: Ruby
    Replies:
    1
    Views:
    161
    George Ogata
    Apr 15, 2006
  3. Srijayanth Sridhar
    Replies:
    19
    Views:
    670
    David A. Black
    Jul 2, 2008
  4. wana
    Replies:
    6
    Views:
    170
    Tad McClellan
    Nov 30, 2004
  5. Brian McCauley

    Replace substring in all values of a hash array?

    Brian McCauley, May 23, 2005, in forum: Perl Misc
    Replies:
    4
    Views:
    168
    michael
    May 23, 2005
Loading...

Share This Page