Forum
2D Array Errors - Printable Version

+- Forum (https://www.boriel.com/forum)
+-- Forum: Compilers and Computer Languages (https://www.boriel.com/forum/forumdisplay.php?fid=12)
+--- Forum: ZX Basic Compiler (https://www.boriel.com/forum/forumdisplay.php?fid=11)
+---- Forum: Bug Reports (https://www.boriel.com/forum/forumdisplay.php?fid=15)
+---- Thread: 2D Array Errors (/showthread.php?tid=2610)



2D Array Errors - raymondlesley - 02-13-2025

I've been enjoying using ZX BASIC so far. It's great to have something that's easy to use (and in-keeping with the original Sinclair BASIC). I have come up against an issue which I can't find a solution for:

The compiler supports multi-dimensional arrays, but it throws an error if I attempt to get a "slice" of the data.

My code:

Code:
DIM data_array(0 TO 2, 0 TO 3) AS UByte = { _
    {0, 1, 2, 3}, {4, 5, 6, 7}, {8, 9, 10, 11} _
}

DIM element12 AS UByte = data_array(1, 2)

DIM element0(0 TO 3) AS UByte

element0 = data_array(0)  ' throws an error

I get no errors relating to element12
I would expect that element0 should result in {0, 1, 2, 3}. It's the right "shape". Instead I get an error:

Quote:
array_test.bas:9: error: Syntax Error. Unexpected token '(' <LP>

I've tried variants e.g. data_array(0, : ) or data_array(0,0 TO 3). Nothing seems to work.

I'm using ZX BASIC 1.17.2


RE: 2D Array Errors - boriel - 02-16-2025

Was that feature actually allowed in Sinclair Basic??
The error is expected. You cannot do that, only copying entire arrays (of the same dimension) onto another.
However it's a nice feature. Will thing of implementing it...

In the meantime, you can create a function to do this (arrays can be passed as parameters) or do it directly at low level:

Code:
#include <memcopy.bas>
DIM data_array(0 TO 2, 0 TO 3) AS UByte = { _
    {0, 1, 2, 3}, {4, 5, 6, 7}, {8, 9, 10, 11} _
}

DIM element12 AS UByte = data_array(1, 2)
DIM element0(0 TO 3) AS UByte

REM element0 = data_array(0)  ' throws an error
MemCopy(@data_array(0, 0), @element0(0), 4)