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


Groups > comp.lang.ruby > #3205 > unrolled thread

Can I check contain value in hash?

Started bySiratinee Sukachai <ploy.sukachai@gmail.com>
First post2011-04-19 23:02 -0500
Last post2011-04-21 02:12 -0500
Articles 6 — 4 participants

Back to article view | Back to comp.lang.ruby


Contents

  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

#3205 — Can I check contain value in hash?

FromSiratinee Sukachai <ploy.sukachai@gmail.com>
Date2011-04-19 23:02 -0500
SubjectCan I check contain value in hash?
Message-ID<4c6b84f27beab6e6f15f2d315a4bef87@ruby-forum.com>
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/.

[toc] | [next] | [standalone]


#3219

FromJesús Gabriel y Galán <jgabrielygalan@gmail.com>
Date2011-04-20 02:49 -0500
Message-ID<BANLkTinHV63ORGKHVBwp2NwBicfUmW6Nwg@mail.gmail.com>
In reply to#3205
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);
> }

You can try the code. A similar approach to the c# above could use
methods Hash#has_key?
(http://ruby-doc.org/core-1.8.7/classes/Hash.html#M000515),
Array#include (http://ruby-doc.org/core-1.8.7/classes/Array.html#M000343)
and Array#<< (http://ruby-doc.org/core-1.8.7/classes/Array.html#M000306).

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.

[toc] | [prev] | [next] | [standalone]


#3222

FromBrian Candler <b.candler@pobox.com>
Date2011-04-20 03:33 -0500
Message-ID<eb48768a2329cb68d8a781177e83fd01@ruby-forum.com>
In reply to#3219
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/.

[toc] | [prev] | [next] | [standalone]


#3224

FromRobert Klemme <shortcutter@googlemail.com>
Date2011-04-20 03:50 -0500
Message-ID<BANLkTi=u-iEduLvX=bRBC2-ATA69kWi8QA@mail.gmail.com>
In reply to#3222
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)

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

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

[toc] | [prev] | [next] | [standalone]


#3248

FromBrian Candler <b.candler@pobox.com>
Date2011-04-20 10:25 -0500
Message-ID<d06a4768babf4824899a76c5167f174f@ruby-forum.com>
In reply to#3224
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/.

[toc] | [prev] | [next] | [standalone]


#3293

FromRobert Klemme <shortcutter@googlemail.com>
Date2011-04-21 02:12 -0500
Message-ID<BANLkTinVzp3WpAMyzUmQ3an=9rajwVYprA@mail.gmail.com>
In reply to#3248
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.

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. :-)

Kind regards

robert

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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.ruby


csiph-web