Can I check contain value in hash?

I have hash which is contain list of string value.
I need to check is the value already contained in the key.
Add add the value to the key if the key is not contained that value.
Can I do that?

like in c#

if(!dict[key].contains(value))
{
  dict[key].add(value);
}

···

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

You can try the code. A similar approach to the c# above could use
methods Hash#has_key?
(Class: Hash (Ruby 1.8.7)),
Array#include (Class: Array (Ruby 1.8.7))
and Array#<< (Class: Array (Ruby 1.8.7)).

Although if your arrays can grow and so you are checking include? many
times on big arrays, you might want to use a Set instead of an Array.

Jesus.

···

On Wed, Apr 20, 2011 at 6:02 AM, Siratinee Sukachai <ploy.sukachai@gmail.com> wrote:

I have hash which is contain list of string value.
I need to check is the value already contained in the key.
Add add the value to the key if the key is not contained that value.
Can I do that?

like in c#

if(!dict[key].contains(value))
{
dict[key].add(value);
}

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/.

My first choice would be to use Set and not Hash as values.

require 'set'

dict = Hash.new {|h,k| h[k] = Set.new}

dict[key] << val
dict[key].include? val

Kind regards

robert

···

On Wed, Apr 20, 2011 at 10:33 AM, Brian Candler <b.candler@pobox.com> wrote:

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)

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

Robert K. wrote in post #993961:

My first choice would be to use Set and not Hash as values.

OK, although Set is really just a thin wrapper around Hash(*), and I
find it easier to work with objects of one class instead of a two.

Regards,

Brian.

(*) example methods from class Set:

  def size
    @hash.size
  end

  def empty?
    @hash.empty?
  end

  def clear
    @hash.clear
    self
  end

  def to_a
    @hash.keys
  end

  def include?(o)
    @hash.include?(o)
  end

  def add(o)
    @hash[o] = true
    self
  end

  def delete(o)
    @hash.delete(o)
    self
  end

···

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

In this case the difference is probably rather small but generally I
try to use the best abstraction. In this case "a collection which
contains every element at most once" translates directly to "set" for
me. :slight_smile:

Kind regards

robert

···

On Wed, Apr 20, 2011 at 5:25 PM, Brian Candler <b.candler@pobox.com> wrote:

Robert K. wrote in post #993961:

My first choice would be to use Set and not Hash as values.

OK, although Set is really just a thin wrapper around Hash(*), and I
find it easier to work with objects of one class instead of a two.

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/