Posts: 806
Threads: 135
Joined: Apr 2009
Reputation:
5
Boriel, can I use Dim in a SUB to reinitialise a global array?
DIM grid (3,3) as uByte
SUB reinitialiseGrid ()
DIM grid (3,3) as uByte => { {0,1,2,3}, {4,5,6,7}, {8,9,10,11}, {12,13,14,15} }
END SUB
Is that legal, or is it trying to make a local variable?
Posts: 806
Threads: 135
Joined: Apr 2009
Reputation:
5
Experimenting says it's a local copy
Must be some way to reinitialise this. Is that a use for "byref" ?
Posts: 1,791
Threads: 56
Joined: Aug 2019
Reputation:
25
Passing arrays in a parameter is not supported (yet).
The best way to do this is:
Code: DIM MyArray As ... => ...
...
...
MyOtherArray = MyArray 'Copies MyArray into MyOtherArray
Is that what you meant?
Posts: 806
Threads: 135
Joined: Apr 2009
Reputation:
5
That will work. Didn't realise it was that simple to make a copy of the array - can make a working copy, and recopy over whenever we want to start again. (I was thinking I might have to do something manual with memcopy @address...)
Very nice, Boriel. ZX Basic never ceases to amaze me as to how powerful it is - and you keep adding more!
Posts: 806
Threads: 135
Joined: Apr 2009
Reputation:
5
Code: DIM grid (4) as uByte => {0,1,2,3,4}
gridcopy=grid
Cannot assign an array to an escalar variable
Hmm.
Okay. If I dim gridcopy first, that bit works - but
1> It fails to copy the data. the first print out of gridcopy still shows 0,0,0,0
2> It fails to compile at the second copy, when I try to reinitialise it again: 40: Array 'grid' has 1 dimensions, not 0
Code: DIM grid (4) as uByte => {0,1,2,3,4}
DIM gridcopy(4) as uByte
gridcopy=grid
ink 1
for n=0 to 4
print grid (n);" ";
next n
print
print
ink 2
for n=0 to 4
print gridcopy (n);" ";
next n
print
print
gridcopy(0)=10
gridcopy(1)=20
gridcopy(2)=30
gridcopy(3)=40
ink 1
for n=0 to 4
print grid (n);" ";
next n
print
print
ink 2
for n=0 to 4
print gridcopy (n);" ";
next n
gridcopy=grid
print
print
ink 1
for n=0 to 4
print grid (n);" ";
next n
print
print
ink 2
for n=0 to 4
print gridcopy (n);" ";
next n
Posts: 1,791
Threads: 56
Joined: Aug 2019
Reputation:
25
This is probably a bug (not a very tested feature, I guess).
Will check it, and see...
Posts: 1,791
Threads: 56
Joined: Aug 2019
Reputation:
25
Ack: this is a bug. Fixing...
Update: I think It's now fixed.
Can you check it? ( download 1.2.8 s785 or newer)
Posts: 806
Threads: 135
Joined: Apr 2009
Reputation:
5
I don't think so.
1.2.9-s798
grid = 0,1,2,3,4
gridcopy = grid
print gridcopy gives 0,0,0,0,0
So no, data still isn't being copied from one array to the other.
Posts: 1,791
Threads: 56
Joined: Aug 2019
Reputation:
25
Ok. Try now, downloading 1.2.9s801. I've fixed more cases (complete test coverage).
Posts: 806
Threads: 135
Joined: Apr 2009
Reputation:
5
The above quoted code now does indeed show the array being copied correctly, changed correctly, and then recopied over correctly.
Awesome. Thanks!
|