arrays

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

arrays

Mad Mountain
could somone show this brain dead new user how to make a multidemensional
array. to access and put in and how to include in the array points or even
how to make an array or points.

respectfully: kelly


Reply | Threaded
Open this post in threaded view
|

Re: arrays

Ted Bracht-2
two-dimensional array:
d2 := #(#(1 2 3) #(3 2 1) #(4 5 6)).
(d2 at: 2) at: 3.

three-dimensional:
d3 := #(#(#(1 2 3) #(3 2 1)) #(#(4 5 6) #(6 5 4)))
((d3 at: 2) at: 1) at: 3

Point array:

p := #((1@2) (2@2) (2@1))
(p at: 2) x

HTH

Ted
www.tedbracht.co.uk



"Mad Mountain" <[hidden email]> wrote in message
news:NpP2a.494$[hidden email]...
> could somone show this brain dead new user how to make a multidemensional
> array. to access and put in and how to include in the array points or even
> how to make an array or points.
>
> respectfully: kelly
>
>


Reply | Threaded
Open this post in threaded view
|

Re: arrays

Ian Bartholomew-18
Ted,

> Point array:
>
> p := #((1@2) (2@2) (2@1))
> (p at: 2) x

Ummm?

--
Ian


Reply | Threaded
Open this post in threaded view
|

Re: arrays

Ian Bartholomew-18
Ooops missed off the smiley (which I trust you realise was intended).   Here
it is anyway

:-)

--
Ian


Reply | Threaded
Open this post in threaded view
|

Re: arrays

Ian Bartholomew-18
In reply to this post by Mad Mountain
Kelly,

> could somone show this brain dead new user how to make a
> multidemensional array. to access and put in and how to include in
> the array points or even how to make an array or points.

Just to expand a bit on Ted's reply.  To avoid getting into LISP territory
(resulting in statements with lots of parentheses) it can be a good idea to
wrap the multi dimensional array in a class of it's own.  For example the
following class definition and three methods ...

Object subclass: #ThreeDArray
    instanceVariableNames: 'array'
    classVariableNames: ''
    poolDictionaries: ''
    classInstanceVariableNames: ''

dim: anArray
    array := Array new: (anArray at: 1).
        1 to: (anArray at: 1)
            do:
                [:dim1 |
                    array at: dim1 put: (Array new: (anArray at: 2)).
                    1 to: (anArray at: 2)
                    do:
                        [:dim2 | (array at: dim1) at: dim2 put: (Array new:
(anArray at: 3))]]

at: dim1 at: dim2 at: dim3
    self assert: [array notNil].
    ^((array at: dim1) at: dim2) at: dim3

at: dim1 at: dim2 at: dim3 put: anObject
    self assert: [array notNil].
    ^((array at: dim1) at: dim2) at: dim3 put: anObject

... then allows you to write the following to create a 3d array

tda := ThreeDArray new.
tda dim: #(5 6 7).

...  put things in it
tda at: 2 at: 3 at: 4 put: 'Something'.

.... and get them back
tda at: 2 at: 3 at: 4

... in a way that is easier to read.  You can then experiment with ways to
make it faster (if needed) by modifying the encapsulated class without
breaking it's public interface.  You can also, if you have a specific
purpose in mind, create accessors with more meaningful names that specify
what the argument should be.

BTW.  To use points just, erm, use Points
tda := ThreeDArray new.
tda dim: #(5 6 7).
tda at: 2 at: 3 at: 4 put: 5@10.
tda at: 2 at: 3 at: 4

--
Ian


Reply | Threaded
Open this post in threaded view
|

Re: arrays

Ted Bracht-2
In reply to this post by Ian Bartholomew-18
> > Point array:
> >
> > p := #((1@2) (2@2) (2@1))
> > (p at: 2) x
>
> Ummm?

old habits ....

p := Array with: (1@2) with: (2@2) with: (2@1)
(p at: 2) x

Sorry for the confusion

Ted


Reply | Threaded
Open this post in threaded view
|

Re: arrays

Andy Bower
In reply to this post by Ian Bartholomew-18
Kelly,

> > could somone show this brain dead new user how to make a
> > multidemensional array. to access and put in and how to include in
> > the array points or even how to make an array or points.
>
> Just to expand a bit on Ted's reply.  To avoid getting into LISP territory
> (resulting in statements with lots of parentheses) it can be a good idea
to
> wrap the multi dimensional array in a class of it's own.  For example the
> following class definition and three methods ...
>
> Object subclass: #ThreeDArray
>     instanceVariableNames: 'array'
>     classVariableNames: ''
>     poolDictionaries: ''
>     classInstanceVariableNames: ''

<snip>

You might be wondering why a language such as Smalltalk doesn't have a
"built-in" class for handling multi-dimensional arrays.  After all, when I
was a kid programming in BASIC (and I don't mean the Visual one) I was using
arrays all over the place for games' data structures and things like the
Knight's Tour problem.  Similarly, I seem to remember using quite a lot of,
at least, two-dimensional arrays when later programming in C.  So, is
Smalltalk really so brain dead not to include such array classes.

I don't think so.  If you think about most of the traditional situations
where a multi-dimensional array is needed, often, the array is a
representation of a more complex object.  For example, in the Knight's Tour,
the two-dimensional array is actually a representation of the chess board.
Therefore in Smalltalk circles one would be encouraged to create a
ChessBoard class with an appropriate #rank:#file: accessor method rather
than use a generic array with a rather nebulous #at:at: accessor. Obviously
the ChessBoard class would have to include the code posted above by Ian but
that is very trivial and, at least, forces you to think about the
requirement for a unique class rather than just slapping a standard array in
there.

We have a little saying here at Object Arts, which is that one should think
carefully before using an array as an anonymous data structure (especially
if the array is being used to transport object of different types).  Almost
always one ends up going back and creating a specialised class to replace
the use of the array later.

Best Regards,

Andy Bower
Dolphin Support
http://www.object-arts.com
---
Are you trying too hard?
http://www.object-arts.com/Relax.htm
---