[Newbies] Best way to implement two-dimensional array

John Almberg jalmberg at identry.com
Tue Sep 4 00:38:37 UTC 2007


H'mmm...

The only way I can figure out how to create a nested directory  
literal is to do this:

|t|
t := { #fs -> ({ #C -> 0. #B -> 1. #R -> 2 } as: Dictionary)  } as:  
Dictionary.
(t at: #fs) at: #B.  "Gives '1' when evaluated"

The parens are necessary, as far as I can figure.

Is there any simpler syntax than this? This is acceptable. Just  
wondering if there is a better way.

Or, I'm thinking it might be simpler to leave the literal as an  
array, and do the castings as necessary during access, like this:

|t|
"Create as simple array"
	t := { #fs -> { #C -> 0. #B -> 1. #R -> 0 } }.
"Access as dictionary"
	(((t as: Dictionary) at: #fs) as: Dictionary) at: #B.

The reason being, the actual table will be pretty big, and all those  
parens and :as Dictionary statements will clutter up the data.

By the way, I would never have figured this out without the Workspace  
to play in. Obviously I'm not the first to discover this, but Wow!  
Workspace is a great tool!

I have used Ruby's IRB to experiment with Ruby expressions, and  
interactive Perl and PHP a bit, but even IRB is no where near as  
useful as the Workspace. It's so simple... I wonder why other  
languages haven't adopted something like it?

-- John



On Sep 3, 2007, at 3:26 AM, Bert Freudenberg wrote:

> See
>
> 	http://www.mucow.com/squeak-qref.html#BraceArray
>
> Another common way is to use a literal array #(...) and convert  
> that into a Dictionary with a few statements. That way you use only  
> a single literal.
>
> - Bert -
>
>
> On Sep 2, 2007, at 19:54 , John Almberg wrote:
>
>> Ah... Now that's handy. Though it's not clear, at first glance,  
>> how it works...
>>
>> Those curly braces aren't standard Smalltalk, are they? I need to  
>> Google them.
>>
>> Anyway, thanks for the tip. That will make life much easier... I  
>> was starting to think my table initialization was going to have a  
>> lot of add: statements...
>>
>> -- John
>>
>>
>> On Sep 2, 2007, at 3:05 PM, Bert Freudenberg wrote:
>>
>>> No, but this comes close:
>>>
>>> {'key1'->'value1'. 'key2'->'value2'} as: Dictionary.
>>>
>>> - Bert -
>>
>
>
> _______________________________________________
> Beginners mailing list
> Beginners at lists.squeakfoundation.org
> http://lists.squeakfoundation.org/mailman/listinfo/beginners

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Websites for On-line Collectible Dealers
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Identry, LLC
John Almberg
(631) 546-5079
jalmberg at identry.com
www.identry.com
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~




More information about the Beginners mailing list