Creating an Array

Monkey Forums/Monkey Programming/Creating an Array

c.k.(Posted 2011) [#1]
I want to create an array of arrays, and I'm initializing like this:

gameGrids = [
	[ [ [1],[2] ], [ [3],[4] ] ]
]


but it's giving me a syntax error on line 2, "Expected ']'" That expected bracket is on line 3. Is that really illegal? Is the opening bracket on line 1 illegal?

Ultimately, I'd like to do something like this:

gameGrids =
[
	[ [ [1],[2] ], [ [3],[4] ] ],
	[ [ [2],[1] ], [ [3],[4] ] ],
	[ [ [1],[2] ], [ [4],[3] ] ],
	[ [ [2],[1] ], [ [4],[3] ] ]
]


so that I have 4 different grids in the set, and I can choose the current one with

currGrid = gameGrids[X].


This is just an example. My grids ultimately will get to up to 3 rows and 3 columns. The point here is, can the opening and/or closing brackets be on separate lines as shown above?


skid(Posted 2011) [#2]
You can end a line with a comma or an opening bracket but not a nested closing bracket which is a pity in regards to formatting, perhaps Mark could look into a "fix".


c.k.(Posted 2011) [#3]
Thanks for the info, skid.

Is that somewhere in the docs? I'd like to get more familiar with finding my own answers so I don't have to clutter up the forum. :)


Samah(Posted 2011) [#4]
Or, if semicolons were mandatory, you could end a line with whatever you wanted. :)


muddy_shoes(Posted 2011) [#5]
True, but languages like Python manage to handle this sort of formatting without them.


Samah(Posted 2011) [#6]
True, but languages like Python manage to handle this sort of formatting without them.

If you like using whitespace for your syntax, sure.
This is the granddaddy of Python: http://en.wikipedia.org/wiki/Whitespace_%28programming_language%29


muddy_shoes(Posted 2011) [#7]
Whether or not I like Python's significant whitespace isn't the point. The point is that it doesn't need a semicolon to denote the end of a statement to allow you to format an array definition across several lines and with indents of your choice. The array brackets delimit the definition quite adequately.