Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Dynamically creating a multi dimensional hash

Reply
Thread Tools

Dynamically creating a multi dimensional hash

 
 
David Sainte-claire
Guest
Posts: n/a
 
      08-11-2009
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", rice => "$ZZZZ.ZZ"}, {:sku => "YYYY",
rice => "$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/.

 
Reply With Quote
 
 
 
 
Robert Klemme
Guest
Posts: n/a
 
      08-11-2009
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", rice => "$ZZZZ.ZZ"}, {:sku => "YYYY",
> rice => "$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, rice

Kind regards

robert

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 
Reply With Quote
 
 
 
 
Joel VanderWerf
Guest
Posts: n/a
 
      08-11-2009
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", rice => "$ZZZZ.ZZ"}, {:sku => "YYYY",
> rice => "$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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
hash of hash of hash of hash in c++ rp C++ 1 11-10-2011 04:45 PM
multi-dimensional arrays to 2-dimensional arrays Wirianto Djunaidi Ruby 2 04-29-2008 07:31 AM
End of my tether trying to retrieve keys from multi-dimensional hash guytew@googlemail.com Perl Misc 4 03-13-2008 04:07 PM
Dynamically Allocated Multi-Dimensional Arrays bwaichu@yahoo.com C Programming 6 09-03-2006 06:05 AM
Sorting a multi-dimensional hash ifiaz Perl Misc 5 11-13-2003 03:08 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57