Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Re-Dim (*solved*)
#1
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?
Reply
#2
Experimenting says it's a local copy Sad

Must be some way to reinitialise this. Is that a use for "byref" ?
Reply
#3
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?
Reply
#4
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!
Reply
#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
Reply
#6
This is probably a bug (not a very tested feature, I guess).
Will check it, and see...
Reply
#7
Ack: this is a bug. Fixing...

Update: I think It's now fixed.
Can you check it? (download 1.2.8 s785 or newer)
Reply
#8
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.
Reply
#9
Ok. Try now, downloading 1.2.9s801. I've fixed more cases (complete test coverage).
Reply
#10
The above quoted code now does indeed show the array being copied correctly, changed correctly, and then recopied over correctly.

Awesome. Thanks!
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)