newbie, hash or 2dArry

Discussion in 'Ruby' started by Zac Elston, Mar 30, 2007.

  1. Zac Elston

    Zac Elston Guest

    I'm lost or just tired from guessing.

    I need to make an hash I can call by hostname as the key and some random
    amount of data after that as the values. now a hash works great for 1
    to 1 maps (h[serverX] ==> result) but I need
    h[serverX][query|result|time]

    so how would I write a hash that would give me access to

    in:
    h[host_to_query]["query"]] = @query
    h[host_to_query]["result"]] = queryhost(host_to_query,@query)

    out:
    for h.each |host| puts h[[host]["result"]]

    thanks

    zaq

    --
    Posted via http://www.ruby-forum.com/.
     
    Zac Elston, Mar 30, 2007
    #1
    1. Advertising

  2. Alle venerd=C3=AC 30 marzo 2007, Zac Elston ha scritto:
    > I'm lost or just tired from guessing.
    >
    > I need to make an hash I can call by hostname as the key and some random
    > amount of data after that as the values. now a hash works great for 1
    > to 1 maps (h[serverX] =3D=3D> result) but I need
    > h[serverX][query|result|time]
    >
    > so how would I write a hash that would give me access to
    >
    > in:
    > h[host_to_query]["query"]] =3D @query
    > h[host_to_query]["result"]] =3D queryhost(host_to_query,@query)
    >
    > out:
    > for h.each |host| puts h[[host]["result"]]
    >
    > thanks
    >
    > zaq


    If I understand correctly, you can create a class with the required instanc=
    es
    variables (or use Struct or OpenStruct) and store query and result there. F=
    or
    instance, using OpenStruct:

    require 'ostruct'

    h[host_to_query] =3DOpenStruct.new:)query=3D> @query, :result=3D queryhost(=
    host_to_query,@query)

    then

    h.each_value{|v| puts v.result}

    =46or more information, you can look at the ri documentation for Hash, Stru=
    ct=20
    and OpenStruct (ri Hash, ri Struct, ri OpenStruct).

    I hope this helps

    Stefano
     
    Stefano Crocco, Mar 30, 2007
    #2
    1. Advertising

  3. Zac Elston

    Keith Tom Guest

    Zac Elston wrote:
    > I'm lost or just tired from guessing.
    >
    > I need to make an hash I can call by hostname as the key and some random
    > amount of data after that as the values. now a hash works great for 1
    > to 1 maps (h[serverX] ==> result) but I need
    > h[serverX][query|result|time]
    >
    > so how would I write a hash that would give me access to
    >
    > in:
    > h[host_to_query]["query"]] = @query
    > h[host_to_query]["result"]] = queryhost(host_to_query,@query)
    >
    > out:
    > for h.each |host| puts h[[host]["result"]]
    >
    > thanks
    >
    > zaq
    >
    >

    Hey Zac,

    Would a nested hash do the trick?

    hash = { :host1 => {
    :query => @query,
    :result => queryhost(...),
    :time => time} }


    then for output, you'd do something like
    for hash.each_value do |value| puts value[:result] end

    Hopefully that does the trick,
    Keith
     
    Keith Tom, Mar 30, 2007
    #3
  4. On 30.03.2007 18:54, Zac Elston wrote:
    > I'm lost or just tired from guessing.
    >
    > I need to make an hash I can call by hostname as the key and some random
    > amount of data after that as the values. now a hash works great for 1
    > to 1 maps (h[serverX] ==> result) but I need
    > h[serverX][query|result|time]
    >
    > so how would I write a hash that would give me access to
    >
    > in:
    > h[host_to_query]["query"]] = @query
    > h[host_to_query]["result"]] = queryhost(host_to_query,@query)


    This is syntactically incorrect since oyu have one closing bracket too much.

    > out:
    > for h.each |host| puts h[[host]["result"]]


    Here's an alternative:

    Info = Struct.new :query, :result, :time
    h = Hash.new {|h,k| h[k] = Info.new}
    h[serverX].query = @query
    h[serverX].result = queryhost(host_to_query,@query)
    h.each {|ho,inf| puts inf.result}

    Of course you can use Arrays instead of Info but the code with Info is
    more readable and less error prone.

    Kind regards

    robert
     
    Robert Klemme, Mar 30, 2007
    #4
  5. Zac Elston

    Zac Elston Guest

    Re: newbie, hash or 2dArry + threads

    first, thanks for the responses, ruby really is a great language.

    My delima is that I'm trying to multithread the actions and store the
    results in a hash with the hostname as key

    to redefine the issue, for each host I need a thread and I expect a
    result, then I'd like to be able to see the result in a
    hash[host][result]

    but I'm having trouble mixing the hash with the thread. maybe I'm going
    about this all wrong. (this is outputting to a rails view, which is why
    i have "@vars"

    I have

    @threads = []
    @resulthash = Hash.new(0)
    @hostarray.each do |host|
    threads << Thread.new(host) do |myhost|
    @resulthash = { :myhost => {
    :packages => @mypackages,
    :result => doXMLquery(host,@mypackages)} }
    logger.info("host = " + host + ", result = " +
    @resulthash[:myhost][:result])
    end
    end

    @threads.each {|thr| thr.join }

    logger.info("result of threads..")
    @hostarray.each do |myhost|
    logger.info("host = " + myhost + ", result = " +
    @resulthash[:myhost][:result])
    end

    logger output

    host = hostX, result = --data returned from hostX--
    host = hostY, result = --data returned from hostY--
    result of threads..
    host = hostX, result = --data returned from hostY--
    host = hostY, result = --data returned from hostY--

    I'm clearly loosing context of the [:host][:result] in the final two
    lines.

    any pointers?

    thanks
    -zaq

    --
    Posted via http://www.ruby-forum.com/.
     
    Zac Elston, Apr 5, 2007
    #5
  6. Zac Elston

    Zac Elston Guest

    Re: newbie, hash or 2dArry + threads

    I used Openstruct and it appears to give me what I want. I'm still
    interested in why a nested hash didn't work if anyone knows.

    thanks again
    -zaq

    --
    Posted via http://www.ruby-forum.com/.
     
    Zac Elston, Apr 5, 2007
    #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. Red Orchid
    Replies:
    3
    Views:
    1,092
  2. Pieter Claassen
    Replies:
    1
    Views:
    1,151
    CBFalconer
    Aug 4, 2004
  3. Bo Peng
    Replies:
    4
    Views:
    817
  4. rp
    Replies:
    1
    Views:
    587
    red floyd
    Nov 10, 2011
  5. Srijayanth Sridhar
    Replies:
    19
    Views:
    673
    David A. Black
    Jul 2, 2008
Loading...

Share This Page