Using new with arrays of arrays

BlitzMax Forums/BlitzMax Beginners Area/Using new with arrays of arrays

sswift(Posted 2006) [#1]
I've been having success using new with arrays that have a single element:

Field Scrambled:Int[]
Scrambled = New Int[WordsToDescramble]

But I can't for the life of me figure out how to use New with an array of errays!

Field Scrambled_Dest:Int[][]

None of the following work:
Scrambled_Dest = New Int[WordsToDescramble]
Scrambled_Dest = New Int[WordsToDescramble][]
Scrambled_Dest = New Int[WordsToDescramble][0]
Scrambled_Dest = New Int[WordsToDescramble]Int[]
Scrambled_Dest[] = New Int[WordsToDescramble]
Scrambled_Dest[] = New Int[WordsToDescramble][]


I want the first array to be resized to WordsToDescramble, and the second set of arrays to be resized like so:

For Loop = 0 To WordsToDescramble-1
Scrambled_Dest[Loop] = New Int[Len(Scrambled$[Loop])]
Next

So each has the same number of elements as the string in question has letters.

However, I don't know if even that part works since I can't get the first bit to work!

I saw some stuff about slices that does array of array resizing, but I wanted to use New because it will clear the arrays and the code would be cleaner.


sswift(Posted 2006) [#2]
Ah I think I figured it out. Someone was having a problem and they did something like this and said it seemed backwards:

Scrambled_Dest = New Int[][WordsToDescramble]

This seems to work. I haven't yet tested it, but it compiles.

I think I understand why it works like this. You are asking for an array of size [WordsToDescramble] of Arrays of Int. "Array of Int" is the type, just as in Int[10] int is the type, so it goes before the array size declaration, [WordsToDescramble].

New Int[WordsToDescramble][] does not work, because you're telling New to make a new array of Int[WordsToDescramble]s, but not specifying how many of those you want.

But then to access it, I guess you have to access it backwards? Like so? Blah[Word][Letters]

That's my best guess at least right now.


Azathoth(Posted 2006) [#3]
This works

Local i:Int[][]

i=New Int[][100]

For x=0 To 99
	i[x]=New Int[10]
Next

For x=0 To 99
	For y=0 To 9
		i[x][y]=0
	Next
Next



Jesse(Posted 2006) [#4]
I use slices to set it up and works fine.
local mw=10 'width
local mh=10 'hight
local maze[][]
maze= maze[..mw]
For Local h = 0 To mw-1
      map.maze[h]=map.maze[h][..mh]
Next