[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Multiple values for the same key in a Hash

Dominic Son

10/12/2006 6:22:00 PM

Hi. There's got to be an easy way to do this.

I simply want multiple iteams (etc, price, quantity, and name) appended
to a key..

There's got to be a simple way (...right?)

--
Posted via http://www.ruby-....

5 Answers

dpenney@gmail.com

10/12/2006 6:32:00 PM

0


Dominic Son wrote:
> Hi. There's got to be an easy way to do this.
>
> I simply want multiple iteams (etc, price, quantity, and name) appended
> to a key..
>
> There's got to be a simple way (...right?)
>
> --
> Posted via http://www.ruby-....

Just add the items as an array or another container object.

Robert Head

10/12/2006 6:40:00 PM

0

Dominic Son wrote:
> Hi. There's got to be an easy way to do this.
>
> I simply want multiple iteams (etc, price, quantity, and name) appended
> to a key..
>
> There's got to be a simple way (...right?)

Uh... Wouldn't a hash of hashes do the trick?

For example,
my_hash[:key][:price] = 1.99
my_hash[:key][:quantity] = 5
...

or

my_hash[:key] = { :price => 1.99, :quantity => 5 }

or whatever the syntax is.

--
Posted via http://www.ruby-....

Dominic Son

10/12/2006 6:56:00 PM

0

Yes, i was thinking your later suggestion, but how do i iterate through
them..

the price, and quanity come back too containated (ie : 1.995 ) do i have
to mess with a .split and reg exp at this point? please say it isn't so!




Robert Head wrote:
> Dominic Son wrote:
>> Hi. There's got to be an easy way to do this.
>>
>> I simply want multiple iteams (etc, price, quantity, and name) appended
>> to a key..
>>
>> There's got to be a simple way (...right?)
>
> Uh... Wouldn't a hash of hashes do the trick?
>
> For example,
> my_hash[:key][:price] = 1.99
> my_hash[:key][:quantity] = 5
> ...
>
> or
>
> my_hash[:key] = { :price => 1.99, :quantity => 5 }
>
> or whatever the syntax is.


--
Posted via http://www.ruby-....

Robert Klemme

10/13/2006 9:10:00 AM

0

On 12.10.2006 20:22, Dominic Son wrote:
> Hi. There's got to be an easy way to do this.
>
> I simply want multiple iteams (etc, price, quantity, and name) appended
> to a key..
>
> There's got to be a simple way (...right?)

Like this?

>> 11:09:28 [~]: irbs
>> require 'pp'
=> true
>> Entry = Struct.new(:price, :quantity, :name)
=> Entry
>> h = Hash.new {|h,k| h[k] = Entry.new}
=> {}
>> h[:foo].price = 10
=> 10
>> h[:bar].name = "bar"
=> "bar"
>> h[:foo].name = "foo"
=> "foo"
>> pp h
{:bar=>#<struct Entry price=nil, quantity=nil, name="bar">,
:foo=>#<struct Entry price=10, quantity=nil, name="foo">}
=> nil

robert

Robert Klemme

10/13/2006 9:28:00 PM

0

Robert Klemme wrote:
> On 12.10.2006 20:22, Dominic Son wrote:
>> Hi. There's got to be an easy way to do this.
>>
>> I simply want multiple iteams (etc, price, quantity, and name)
>> appended to a key..
>>
>> There's got to be a simple way (...right?)
>
> Like this?

PS: Alternatively you can use OpenStruct.

robert