Re: Question: Arrays and Matrixes
Re: Question: Arrays and Matrixes
- Subject: Re: Question: Arrays and Matrixes
- From: Brennan <email@hidden>
- Date: Tue, 12 Mar 2002 22:47:02 +0100
Timothy Bates <email@hidden> wrote:
>
> I'm starting to try to play around with construction and filling of
>
> multi-dimensional arrays/matrixes, but I can't find or figure out the correct
>
> verbage to create one. Specifically, I'd like to create a 4 dimensional array
>
> (100x250x320x160) filled with datatype character.
>
>
>
> But, for clarification, a two dimensional one should work... For example, if
>
> you envision a tic-tac-toe board (a grossly simplified example) as a 3 x 3
>
> array of type character, with each data containing either nothing, X or O,
>
> how would I create this array?
>
>
In AppleScript arrays are lists. Multidimensional arrays are lists of lists.
>
Yo can mix types. Anyhow. Tic-tac-to =
>
>
Set theBoard to{{"","",""},{"","",""},{"","",""}}
>
>
on mymove()
>
set item 1 of item 2 of theBoard to "X"
>
end mymove
>
>
-->{{"","",""},{"X","",""},{"","",""}}
Sure, lists of lists can represent multidimensional arrays of arbitrary complexity, but *creating* a multidimensional array of arbitrary dimensions, such as 100x250x320x160 can be quite tricky. The most flexible way to do it is to use recursion.
I wrote an article about recursion in relation to Lingo, which also has lists rather than arrays, and whose syntax is very close to applescript. You can read it at
<
http://www.director-online.com/accessArticle2.cfm?id=343>
... and here's the function from that article for generating a multidimensional array translated to applescript:
on generateArray(dimensions, fillval)
if (count dimensions) > 0 then
set car to (item 1 of dimensions)
set inner to {}
copy dimensions to cdr
set cdr to {}
set skipFirstitem to true
repeat with d in dimensions
if skipFirstitem then
set skipFirstitem to false
else
set the end of cdr to d
end if
end repeat
repeat with n from 1 to car
-- go deeper --
set the end of inner to (generateArray(cdr, fillval))
end repeat
return inner
else
-- come back up again --
return fillval
end if
end generateArray
...so to make a list of dimensions (100x250x320x160) filled with empty strings, just use
generateArray({100,250,320,160}, "")
... but beware, this takes a long time to build!
Brennan
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.