Creating hashes with duplicate keys

Discussion in 'Ruby' started by Sy Ali, Jul 28, 2006.

  1. Sy Ali

    Sy Ali Guest

    I want to simplify this hash creation:
    g = { 1 => "test", 2 => "test", }

    I have a particularly complicated hash that I'm generating. It can
    have very large values.

    Right now, I duplicate values with something like this:

    c = { 1 => "one", }
    d = { 2 => c[1], }
    c.merge!(d)

    however, I don't like the way my code looks.. I want to be able to
    specify the duplication within the hash generation.

    Is this possible?
    Sy Ali, Jul 28, 2006
    #1
    1. Advertising

  2. On 28/07/06, Sy Ali <> wrote:
    > I want to simplify this hash creation:
    > g = { 1 => "test", 2 => "test", }
    >
    > I have a particularly complicated hash that I'm generating. It can
    > have very large values.
    >
    > Right now, I duplicate values with something like this:
    >
    > c = { 1 => "one", }
    > d = { 2 => c[1], }
    > c.merge!(d)
    >
    > however, I don't like the way my code looks.. I want to be able to
    > specify the duplication within the hash generation.
    >
    > Is this possible?


    Something like this?
    irb(main):001:0> c={}
    => {}
    irb(main):002:0> c[1] = "one"
    => "one"
    irb(main):003:0> c[2] = c[1]
    => "one"
    irb(main):004:0> c[1].object_id == c[2].object_id
    => true

    Farrel
    Farrel Lifson, Jul 28, 2006
    #2
    1. Advertising

  3. Sy Ali wrote:
    > I want to simplify this hash creation:
    > g = { 1 => "test", 2 => "test", }
    >
    > I have a particularly complicated hash that I'm generating. It can
    > have very large values.
    >
    > Right now, I duplicate values with something like this:
    >
    > c = { 1 => "one", }
    > d = { 2 => c[1], }
    > c.merge!(d)
    >
    > however, I don't like the way my code looks.. I want to be able to
    > specify the duplication within the hash generation.
    >
    > Is this possible?


    This will work:

    hsh = Hash.new{|h, k| h[k] = (k == 1 ? "one" : h[k - 1])}

    Note that all the values will be the same -- the same object! If you
    want duplicates, do this instead:

    hsh = Hash.new{|h, k| h[k] = (k == 1 ? "one" : h[k - 1].dup)}

    Just ask if there are parts of the snippet you don't understand.


    Cheers,
    Daniel
    Daniel Schierbeck, Jul 28, 2006
    #3
  4. Sy Ali

    Peña, Botp Guest

    fr Sy:
    # I want to simplify this hash creation:
    # g =3D { 1 =3D> "test", 2 =3D> "test", }
    # ...
    # however, I don't like the way my code looks.. I want to be able to
    # specify the duplication within the hash generation.

    not sure if this simple idea will help, but,

    irb(main):055:0> class Hash
    irb(main):056:1> def init2 range
    irb(main):057:2> range.each{|c| self[c]=3Dself.default}
    irb(main):058:2> end
    irb(main):059:1> end
    =3D> nil
    irb(main):060:0> h=3DHash.new
    =3D> {}
    irb(main):065:0> h.default =3D "test"
    =3D> "test"
    irb(main):066:0> h.init2 1..8
    =3D> 1..8
    irb(main):067:0> h
    =3D> {5=3D>"test", 6=3D>"test", 1=3D>"test", 7=3D>"test", 2=3D>"test", =
    8=3D>"test", 3=3D>"test", 4=3D>"test"}
    irb(main):068:0> h.default =3D "testing again"
    =3D> "testing again"
    irb(main):069:0> h.init2 6..10
    =3D> 6..10
    irb(main):070:0> h
    =3D> {5=3D>"test", 6=3D>"testing again", 1=3D>"test", 7=3D>"testing =
    again", 2=3D>"test", 8=3D>"testing again", 3=3D>"test", 9=3D>"testing =
    again", 4=3D>"test", 10=3D>"testing again"}
    irb(main):071:0>

    kind regards -botp
    Peña, Botp, Jul 28, 2006
    #4
  5. Sy Ali

    Guest

    On Fri, 28 Jul 2006, Sy Ali wrote:

    > I want to simplify this hash creation:
    > g = { 1 => "test", 2 => "test", }
    >
    > I have a particularly complicated hash that I'm generating. It can
    > have very large values.
    >
    > Right now, I duplicate values with something like this:
    >
    > c = { 1 => "one", }
    > d = { 2 => c[1], }
    > c.merge!(d)
    >
    > however, I don't like the way my code looks.. I want to be able to
    > specify the duplication within the hash generation.
    >
    > Is this possible?


    use rbtree (from the raa)

    harp:~ > cat a.rb
    require 'rbtree'

    rb = RBTree.new

    rb[1] = 'test'
    rb[2] = 'test'

    rb.each{|k,v| p [k,v]}


    harp:~ > ruby a.rb
    [1, "test"]
    [2, "test"]

    regards.

    -a
    --
    we can never obtain peace in the outer world until we make peace with
    ourselves.
    - h.h. the 14th dali lama
    , Jul 28, 2006
    #5
  6. Sy Ali

    Guest

    On Fri, 28 Jul 2006 wrote:

    > use rbtree (from the raa)
    >
    > harp:~ > cat a.rb
    > require 'rbtree'
    >
    > rb = RBTree.new
    >
    > rb[1] = 'test'
    > rb[2] = 'test'
    >
    > rb.each{|k,v| p [k,v]}
    >
    >
    > harp:~ > ruby a.rb
    > [1, "test"]
    > [2, "test"]


    yikes - hit send too soon, here ya go:

    harp:~ > cat a.rb
    require 'rbtree'

    rb = MultiRBTree.new

    rb[1] = 'test'
    rb[1] = 'test'

    rb.each{|k,v| p [k,v]}

    p rb.to_a
    p rb[1]


    harp:~ > ruby a.rb
    [1, "test"]
    [1, "test"]
    [[1, "test"], [1, "test"]]
    "test"


    -a
    --
    we can never obtain peace in the outer world until we make peace with
    ourselves.
    - h.h. the 14th dali lama
    , Jul 28, 2006
    #6
    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. Ben Holness

    Hashes of Hashes via subs

    Ben Holness, Oct 5, 2003, in forum: Perl
    Replies:
    8
    Views:
    543
    Ben Holness
    Oct 8, 2003
  2. Steven Arnold

    using hashes as keys in hashes

    Steven Arnold, Nov 23, 2005, in forum: Ruby
    Replies:
    3
    Views:
    152
    Mauricio Fernández
    Nov 23, 2005
  3. kazaam
    Replies:
    12
    Views:
    257
    Matthias Wächter
    Sep 13, 2007
  4. Neela megha shyam Chivukula

    On Hashes - How the hashes printing works?

    Neela megha shyam Chivukula, May 27, 2009, in forum: Ruby
    Replies:
    4
    Views:
    207
    Markus Schirp
    May 28, 2009
  5. Tim O'Donovan

    Hash of hashes, of hashes, of arrays of hashes

    Tim O'Donovan, Oct 27, 2005, in forum: Perl Misc
    Replies:
    5
    Views:
    196
Loading...

Share This Page