[Newbies] Arrays within arrays

Yoshiki Ohshima yoshiki at vpri.org
Fri Sep 5 06:35:31 UTC 2008


At Thu, 4 Sep 2008 15:14:44 +0000 (UTC),
Charles Gray wrote:
> 
> Why doesn't this work?
> 
> strength := Array new: 10.
> strength := #( #(0 0 0 0) #(0 0 0 0) #(0 0 0 0) #(0 0 0 0) #(0 0 0 0) #(0 0 0 0)
> #(2 1 0 0) #( 8 4 0 1) #(14 6 0 2) #(30 12 1 4)).

  Why do you have two assignments here?

  The literal syntax has a bit of context sensitive-ness.  In a
literal array, the # is not necessary to mean nested literal array.
So,

   strength := #((0 0 0 0) (0 0 0 0) (0 0 0 0) (0 0 0 0) (0 0 0 0) (0 0 0 0)
               (2 1 0 0) (8 4 0 1) (14 6 0 2) (30 12 1 4)).

Also works.

> I read that the contents of an array are objects and the arrays inside of
> strength are objects but Smalltalk simply refuses to accept this assignment. I
> didn't want to say a := #(0 0 0 0). b := #(2 1 0 0). c := #(8 4 0 1) ... etc.
> and then strength := #( a a a a a a b c ... etc  but it appears this may be
> necessary. I suppose a, b, c, etc are temporary variables and will disappear
> after use but it seems like a terrible waste.

  It doesn't refuse the assignment, right?  And you can't use a
variable in a literal array to mean the contents of the variable.

  Or, the above is actually a simplified code, and some other object
is holding onto the reference to the "Array new: 10" array?  If you
assign a new array created from the literal array syntax into the same
name variable, the guy who is holding onto the reference to "Array
new: 10" wouldn't see the change.

-- Yoshiki

  Just curious, but what do these numbers represent?


More information about the Beginners mailing list