Growing pains (with arrays)

BlitzMax Forums/BlitzMax Programming/Growing pains (with arrays)

Grey Alien(Posted 2006) [#1]
Please try this code:

Strict

Print "-----"

'single
Local a[]
Print Len(a)
a = a[0..5]
Print Len(a)
Print Len(a[0])
a[0]=20
Print a[0]

Print "-----"

'double 1 
Local b[2,2]
Print Len(b)
'b = b[0..5,0..10] (can't use slices with double arrays) :-(
'Print Len(b[0]) 'incorrect number of dimensions, yep fair enough
Print Len(b[0,0])
b[0,0]=20
Print b[0,0]

Print "-----"

'double 2
Local c[][]
Print Len(c)
c = c[0..5][0..10]
Print Len(c)
Print Len(c[0])
Print Len(c[0][0])
c[0][0]=20 'fails here
Print c[0][0]

Print "-----"


It crashes on c[0][0]=20 with "attempt to index array element beyond length" when you have debug mode on.

Note how with a single array you can grow it (with slicing) and also how the length of a single element is 1. Oh yeah why is Len(a) 5 and not 6 because 0..5 = 6 elements! (oh wait, I remember, slicing is 1 based and zero means null right?)

With a standard 2 dimensional array, you can't slice it (shame). Len returns the full size and a single element is 1.

Now here's the weird one...I was trying to use a double array so that I could grow each part. Note how the Len(c) returns 10, the number of elements in the second part. What's odd is that Len(c[0]) returns 0 because the element is null yet Len(c[0][0]) returns 1 because the element has a length of 1. Then it fails to set c[0][0] because it thinks it's out of bounds. So basically the double slice has failed on the first index, but seems OK on the second index (I think).

Maybe I'm trying to use the compiler on a way it's not designed, but I'd really like to have a 2D array that I can grow and shrink (think tile-based level that you may want to alter whenever it's loaded).


Murilo(Posted 2006) [#2]
[edit]
Sorry - Removed. I thought I'd highlighted an issue with your use of jagged arrays...
[/edit]


Perturbatio(Posted 2006) [#3]
Strict

Print "-----"

'single
Local a[]
Print Len(a)
a = a[0..5]
Print Len(a)
Print Len(a[0])
a[0]=20
Print a[0]

Print "-----"

'double 1 
Local b[2,2]
Print Len(b)
'b = b[0..5,0..10] (can't use slices with double arrays) :-(
'Print Len(b[0]) 'incorrect number of dimensions, yep fair enough
Print Len(b[0,0])
b[0,0]=20
Print b[0,0]

Print "-----"

'double 2
Local c[][]
Print Len(c)
'c = c[0..5][0..10]
c = c[..5]

For Local i:Int = 0 To Len(c)-1
	c[i] = c[i][..10]
Next

Print Len(c)
Print Len(c[0])
Print Len(c[0][0])
c[0][0]=20 'fails here
Print c[0][0]

Print "-----"




BlackSp1der(Posted 2006) [#4]
'Print Len(b[0]) 'incorrect number of dimensions, yep fair enough
Print b.Dimensions()[0]+" , "+b.Dimensions()[1]


Maybe I'm trying to use the compiler on a way it's not designed, but I'd really like to have a 2D array that I can grow and shrink (think tile-based level that you may want to alter whenever it's loaded).


I allways use the 3rd method
array[num][x]
num0. x0,x1,x2,x3
num1. x0,x1
num2. x0,x1,x2,x3,x4


Curtastic(Posted 2006) [#5]
c = c[0..3][0..10]


What does this line actually do in blitzmax?


BlackSp1der(Posted 2006) [#6]

c = c[0..3][0..10]
What does this line actually do in blitzmax?



extend the first array dimension in 3 and 10 later. only the last is preserved.
c=c[0..3]; c=c[0..10]

maybe it's a bug and need a warning message.


Grey Alien(Posted 2006) [#7]
Perturbatio: Thanks very much that's very clear.

BlackSp1der: Interesting. Yeah I think it needs a warning to stop dullards like me from using it.

So here's another thing, is using c[x][y] slower than using c[x,y] for like a tiled scrolling game I wonder?


FlameDuck(Posted 2006) [#8]
So here's another thing, is using c[x][y] slower than using c[x,y] for like a tiled scrolling game I wonder?
Yes, but only marginally so.


Fabian.(Posted 2006) [#9]
'b = b[0..5,0..10] (can't use slices with double arrays) :-(
I posted this as feature request 3 months ago. But there was no answer from brl. It would need just a little change in the compiler and the c-code I posted should be included to brl.mod/blitz.mod/blitz_array.c - then we would have full array slicing support.


Grey Alien(Posted 2006) [#10]
FD: thanks, well it probably won't make difference as I only need to access the array 20,000 per second and the jagged array method (c[x][y]) allows me to change the array size dynamically so is better.

Fabian: Shame as it would be neat, I just expected it to work.