Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Create 2D arrays in smalltalk

i have followed this previuos thread How to manage 2d array in Smalltalk? but it did not helped me please help me out .

I am trying to create NXN array and then print them out . for exmaple 2x2 array : what am i missing ?

|testArr|.

testArr := (Array new: 2)
at: 1 put: ((Array new: 2) at: 1 put: '0'; at: 2 put: 'X');
at: 2 put: ((Array new: 2) at: 1 put: 'P'; at: 2 put: 'Y').

1 to:2 do:[:a|

1 to:2 do:[:b|

Transcript show: testArr at:a at:b.
].
].

THE ERROR IS AT Transcript with unknow selector . what can i do to fix it ?

like image 299
Berko Avatar asked Mar 17 '23 01:03

Berko


1 Answers

There are several issues with your code:

First the error message you describe. I guess in full length it says:

MessageNotUnderstood: ThreadSafeTranscript>>show:at:at:

So this means you should set some parenthesis to get the right messages to the right objects. Try:

Transcript show: ((testArr at:a) at:b).

Now there is an issue with your Array assignments too.

In Smalltalk/Pharo/Squeak, if you send at:put: to an array it returns the object you assigned, the second parameter of at:put:, not the receiver. So in your example the variable testArr doesn't contain an array of arrays, but the string 'Y'.

If you really want to use message cascading with ;, you must send the yourself message to the array at the end of your cascade.

Like this:

testArr := (Array new: 2).
testArr at: 1 put: ((Array new: 2) at: 1 put: '0'; at: 2 put: 'X'; yourself).
testArr at: 2 put: ((Array new: 2) at: 1 put: 'P'; at: 2 put: 'Y'; yourself).
like image 114
MartinW Avatar answered Mar 31 '23 10:03

MartinW