Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.ruby > #3222

Re: Can I check contain value in hash?

From Brian Candler <b.candler@pobox.com>
Newsgroups comp.lang.ruby
Subject Re: Can I check contain value in hash?
Date 2011-04-20 03:33 -0500
Organization Service de news de lacave.net
Message-ID <eb48768a2329cb68d8a781177e83fd01@ruby-forum.com> (permalink)
References <4c6b84f27beab6e6f15f2d315a4bef87@ruby-forum.com> <BANLkTinHV63ORGKHVBwp2NwBicfUmW6Nwg@mail.gmail.com>

Show all headers | View raw


For some applications,

  dict[key] << val
  dict[key].uniq!

will be good enough. But this will still slow down if the number of 
values in the list gets large, in which case a hash of hashes would be 
better:

  dict[key] ||= {}
  dict[key][val] = true

To get the values, you'd then use dict[key].keys (which in ruby 1.8 
would be in an arbitrary order, and in 1.9 would be in order of first 
insertion)

-- 
Posted via http://www.ruby-forum.com/.

Back to comp.lang.ruby | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Can I check contain value in hash? Siratinee Sukachai <ploy.sukachai@gmail.com> - 2011-04-19 23:02 -0500
  Re: Can I check contain value in hash? Jesús Gabriel y Galán <jgabrielygalan@gmail.com> - 2011-04-20 02:49 -0500
    Re: Can I check contain value in hash? Brian Candler <b.candler@pobox.com> - 2011-04-20 03:33 -0500
      Re: Can I check contain value in hash? Robert Klemme <shortcutter@googlemail.com> - 2011-04-20 03:50 -0500
        Re: Can I check contain value in hash? Brian Candler <b.candler@pobox.com> - 2011-04-20 10:25 -0500
          Re: Can I check contain value in hash? Robert Klemme <shortcutter@googlemail.com> - 2011-04-21 02:12 -0500

csiph-web