Dynamically creating a multi dimensional hash

  • Thread starter David Sainte-claire
  • Start date
D

David Sainte-claire

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
 
R

Robert Klemme

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
 
J

Joel VanderWerf

David said:
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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top