[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Re: Multi-dimensioned sparse array ?

Austin Ziegler

11/19/2003 8:50:00 PM

On Thu, 20 Nov 2003 03:43:24 +0900, Charles Hixson wrote:
> Austin Ziegler wrote:
>> hash_maker = proc { |h, k| h[k] = Hash.new(&hash_maker) }
>> msa = Hash.new(&hash_maker)
>>
>> http://www.rubygarden.org/ruby?Ha...

> How would you index along other than the first dimension? But
> that's one of the main requirements.

Using the above:

hash_maker = proc { |h, k| h[k] = Hash.new(&hash_maker) }
msa = Hash.new(&hash_maker)
msa[0] # => {}
msa[0][5] # => {}
msa # => {0=>{5=>{}}
msa[5][3][2] = 3 # => 3
msa # => {5=>{3=>{2=>3}}, 0=>{5=>{}}}

The first pitfall that you'll have is:

msa[0] = 3
msa # => {5=>{3=>{2=>3}}, 0=>3}

The second pitfall is on the HashOfHashes page.

-austin
--
austin ziegler * austin@halostatue.ca * Toronto, ON, Canada
software designer * pragmatic programmer * 2003.11.19
* 15.36.49




1 Answer

Charles Hixson

11/19/2003 10:03:00 PM

0

Austin Ziegler wrote:

>On Thu, 20 Nov 2003 03:43:24 +0900, Charles Hixson wrote:
>
>
>>Austin Ziegler wrote:
>>
>>
>>>hash_maker = proc { |h, k| h[k] = Hash.new(&hash_maker) }
>>>msa = Hash.new(&hash_maker)
>>>
>>>http://www.rubygarden.org/ruby?Ha...
>>>
>>>
>
>
>
>>How would you index along other than the first dimension? But
>>that's one of the main requirements.
>>
>>
>
>Using the above:
>
> hash_maker = proc { |h, k| h[k] = Hash.new(&hash_maker) }
> msa = Hash.new(&hash_maker)
> msa[0] # => {}
> msa[0][5] # => {}
> msa # => {0=>{5=>{}}
> msa[5][3][2] = 3 # => 3
> msa # => {5=>{3=>{2=>3}}, 0=>{5=>{}}}
>
>The first pitfall that you'll have is:
>
> msa[0] = 3
> msa # => {5=>{3=>{2=>3}}, 0=>3}
>
>The second pitfall is on the HashOfHashes page.
>
>-austin
>--
>austin ziegler * austin@halostatue.ca * Toronto, ON, Canada
>software designer * pragmatic programmer * 2003.11.19
> * 15.36.49
>
It seems like I *MUST* be misunderstanding the hash of hashes concept.
But it looks to me as if given:
msa.default = nil
then
msa[2] == nil #as it wasn't defined above
and therefore
msa[2][3] === nil[3]
but nil[3] isn't defined.

So I ran a test on:
# test.rb

hash_maker = proc { |h, k| h[k] = Hash.new(&hash_maker) }
tst = Hash.new(&hash_maker)

tst[3][2] = "able"
x = 3
y = 2
print tst[x][y][3], "\n" # ==>101
#!!!That was a bit of a surprise! And I still don't understand it.
(101.chr == 'e'?!)
# --Later: Of Course. That's an index into the returned string!

#!!!OTOH, it looks like as long as I don't make *ANY* errors it may work.
#!! But I don't understand either it, or how much space it consumes
print "<<#{x}, #{y}>>:#{tst[x][y]} \n"
print "<<#{x}, #{y-1}>>:#{tst[x][y-1]} \n"
==><<3, 2>>:able
==><<3, 1>>:
So that works out correctly. I may need to fake ranges, but are cells
generated if I read them instead of writing to them? If so this isn't
useable, as it will take up too much space. Otherwise....It may just be
the best solution possible. (I can throw a class wrapper around it, so
the interface isn't a problem...much)
Unfortunately, at this point I added the line:
tst.default = nil
right after the declaration.
Whoops! Now tst[3][2] = "able" throws the error that I expected.
I guess that could be fixed by defining the <=> to set default to nil
before testing and back to hashmaker before exit. So I'm starting to
understand the implementation...but not the space constraints yet.