Dynamically creating a multi dimensional hash

Discussion in 'Ruby' started by David Sainte-claire, Aug 11, 2009.

  1. Hello,

    I am trying to do something that on the surface seems easy, but in
    practice I don't have a clue how to approach it. I need to create a
    multi-dimensional hash to represent certain attributes of a particular
    item. When I hardcode the hash for testing purposes it looks something
    like this:

    @skuList = [{:sku => "XXXX", :price => "$ZZZZ.ZZ"}, {:sku => "YYYY",
    :price => "$AAA.AA"}]

    However, in production I need to be able to generate an almost exact
    same hash dynamically, with the exception of that I don't know how many
    objects will need to be inserted into the hash.

    Basically, I will be reading in a file that contains an unknown number
    of item names and need to return hash that contains the above
    attributes for all entries in the file that I just read.

    Unfortunately, I am very new to Ruby, and don't have a clue where to
    start on something like this.

    Can someone help me out?

    Thanks,
    David
    --
    Posted via http://www.ruby-forum.com/.
     
    David Sainte-claire, Aug 11, 2009
    #1
    1. Advertising

  2. On 11.08.2009 21:45, David Sainte-claire wrote:
    > I am trying to do something that on the surface seems easy, but in
    > practice I don't have a clue how to approach it. I need to create a
    > multi-dimensional hash to represent certain attributes of a particular
    > item. When I hardcode the hash for testing purposes it looks something
    > like this:
    >
    > @skuList = [{:sku => "XXXX", :price => "$ZZZZ.ZZ"}, {:sku => "YYYY",
    > :price => "$AAA.AA"}]


    First of all @skuList is not a Hash but an Array.

    > However, in production I need to be able to generate an almost exact
    > same hash dynamically, with the exception of that I don't know how many
    > objects will need to be inserted into the hash.


    Is the number of objects unknown or the level of nesting?

    > Basically, I will be reading in a file that contains an unknown number
    > of item names and need to return hash that contains the above
    > attributes for all entries in the file that I just read.


    Ok, you do not need arbitrary nesting but rather you want to add a
    variable number of items to the Array.

    > Unfortunately, I am very new to Ruby, and don't have a clue where to
    > start on something like this.


    You should have a look at the documentation of class Array. You'll find
    methods for changing an Array's content there.

    Btw, a few other remarks:

    In Ruby we conventionally use identifiers like @sku_list instead of
    @skuList which you rather find in Java. While of course you are free to
    choose it may make your code easier readable for your fellow Ruby
    programmers if you go with that convention.

    If your set of attributes is fixed as seems to be the case it is a good
    idea to use a class for this instead of Hash instances. The easiest
    would be

    SkuItem = Struct.new :sku, :price

    Kind regards

    robert

    --
    remember.guy do |as, often| as.you_can - without end
    http://blog.rubybestpractices.com/
     
    Robert Klemme, Aug 11, 2009
    #2
    1. Advertising

  3. David Sainte-claire wrote:
    > Hello,
    >
    > I am trying to do something that on the surface seems easy, but in
    > practice I don't have a clue how to approach it. I need to create a
    > multi-dimensional hash to represent certain attributes of a particular
    > item. When I hardcode the hash for testing purposes it looks something
    > like this:
    >
    > @skuList = [{:sku => "XXXX", :price => "$ZZZZ.ZZ"}, {:sku => "YYYY",
    > :price => "$AAA.AA"}]
    >
    > However, in production I need to be able to generate an almost exact
    > same hash dynamically, with the exception of that I don't know how many
    > objects will need to be inserted into the hash.
    >
    > Basically, I will be reading in a file that contains an unknown number
    > of item names and need to return hash that contains the above
    > attributes for all entries in the file that I just read.
    >
    > Unfortunately, I am very new to Ruby, and don't have a clue where to
    > start on something like this.
    >
    > Can someone help me out?
    >
    > Thanks,
    > David


    Looks like an array of hashes. What do you mean by "multidimensional hash"?

    There's a ruby idiom that may or may not apply here, making use of the
    default proc of a hash to create arbitrary nesting:

    make_hash = proc do |hash,key|
    hash[key] = Hash.new(&make_hash)
    end

    h = Hash.new(&make_hash)

    h[1][2][3] = "foo"

    p h # => {1=>{2=>{3=>"foo"}}}

    But if you are just trying to construct an array of hashes, something
    like this may work:

    ary = []
    File.open(...).each do |line|
    hash = your_parser(line)
    ary << hash
    end

    Sorry if I've misunderstood!

    --
    vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407
     
    Joel VanderWerf, Aug 11, 2009
    #3
    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. Replies:
    6
    Views:
    443
  2. rp
    Replies:
    1
    Views:
    539
    red floyd
    Nov 10, 2011
  3. Wirianto Djunaidi
    Replies:
    2
    Views:
    203
    Wirianto Djunaidi
    Apr 29, 2008
  4. ifiaz

    Sorting a multi-dimensional hash

    ifiaz, Nov 11, 2003, in forum: Perl Misc
    Replies:
    5
    Views:
    180
    Tad McClellan
    Nov 13, 2003
  5. Replies:
    4
    Views:
    207
    woodyg
    Mar 13, 2008
Loading...

Share This Page