Hash as Checklist

Discussion in 'Ruby' started by basi, Apr 28, 2005.

  1. basi

    basi Guest

    Hello,
    I need to check if a word is in a list. I'm using hash because the
    lists can be long (i'm under the impression hash is faster than array).
    Basically I'm only interested that a key exists. I have to create
    entries like:

    h = {
    "one" => true,
    "two" => true,
    "three" => true,
    "four" => true,
    "five" => true
    }

    So h["one"] gives true, and h["seven"] returns nil. But typing the
    dummy value "true" is a waste of effort, and there's an ugliness in
    there. Is there a better way to design a simple checklist?

    Thank you for your help
    basi
     
    basi, Apr 28, 2005
    #1
    1. Advertising

  2. basi

    Assaph Mehr Guest

    basi wrote:
    > Hello,
    > I need to check if a word is in a list. I'm using hash because the
    > lists can be long (i'm under the impression hash is faster than

    array).
    > Basically I'm only interested that a key exists. I have to create
    > entries like:
    >
    > h = {
    > "one" => true,
    > "two" => true,
    > "three" => true,
    > "four" => true,
    > "five" => true
    > }
    >
    > So h["one"] gives true, and h["seven"] returns nil. But typing the
    > dummy value "true" is a waste of effort, and there's an ugliness in
    > there. Is there a better way to design a simple checklist?


    irb(main):001:0> require 'set'
    => true
    irb(main):002:0> s = Set.new %w{one two three four}
    => #<Set: {"three", "two", "one", "four"}>
    irb(main):005:0> s.member? 'one'
    => true
    irb(main):006:0> s.member? 'seven'
    => false
     
    Assaph Mehr, Apr 28, 2005
    #2
    1. Advertising

  3. basi wrote:
    > I need to check if a word is in a list. I'm using hash because the
    > lists can be long (i'm under the impression hash is faster than array).
    > Basically I'm only interested that a key exists. I have to create
    > entries like:
    >
    > h = {
    > "one" => true,
    > "two" => true,
    > "three" => true,
    > "four" => true,
    > "five" => true
    > }
    >
    > So h["one"] gives true, and h["seven"] returns nil. But typing the
    > dummy value "true" is a waste of effort, and there's an ugliness in
    > there. Is there a better way to design a simple checklist?


    Use a set:
    require 'set'
    words = %w(one two three four five).to_set
    words.include? 'one' # => true
    words.include? 'seven' # => false

    http://www.ruby-doc.org/stdlib/libdoc/set/rdoc/

    jeremy
     
    Jeremy Kemper, Apr 28, 2005
    #3
  4. basi

    basi Guest

    Thanks to both Assaph and Jeremy for the quick reply. This is just what
    I need!

    Cheers!
    basi
     
    basi, Apr 28, 2005
    #4
  5. On 28/04/05, Dr Balwinder S Dheeman <> wrote:
    > On 04/28/2005 07:48 AM, basi wrote:
    > > Hello,
    > > I need to check if a word is in a list. I'm using hash because the
    > > lists can be long (i'm under the impression hash is faster than array).

    >
    > A hash have to store "key" => value pairs, how will be more efficient
    > than an array? and why?
    >


    because Array lookups are O(n) and Hash lookups should be O(1). To
    find something in an Array you have to traverse it completely and will
    find the entry you are searching in expected n/2 steps. For hashes
    there exist different implementations, but basically you compute the
    possible position of an object from the object and just take a look if
    it is there. (Oversimplified).

    best regards,

    Brian

    > [snip]


    --
    http://ruby.brian-schroeder.de/

    multilingual _non rails_ ruby based vocabulary trainer:
    http://www.vocabulaire.org/ | http://www.gloser.org/ | http://www.vokabeln.net/
     
    Brian Schröder, Apr 28, 2005
    #5
    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. sean

    Openmore checklist

    sean, Jan 7, 2004, in forum: VHDL
    Replies:
    2
    Views:
    934
  2. roni
    Replies:
    2
    Views:
    385
    Eliyahu Goldin
    Jul 17, 2005
  3. nidhi

    tix checklist

    nidhi, Jul 17, 2004, in forum: Python
    Replies:
    0
    Views:
    454
    nidhi
    Jul 17, 2004
  4. Andy
    Replies:
    2
    Views:
    495
    Travis Newbury
    Nov 7, 2009
  5. rp
    Replies:
    1
    Views:
    538
    red floyd
    Nov 10, 2011
Loading...

Share This Page