Forum
Clearing an array - 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: Help & Support (https://www.boriel.com/forum/forumdisplay.php?fid=16)
+---- Thread: Clearing an array (/showthread.php?tid=1010)



Clearing an array - patters - 01-19-2021

What is the recommended way to reset an array's contents? Do we have to run a FOR loop from 0 TO UBOUND and clear it element by element, or is there a neater way?
I see that the following doesn't work:

Code:
arrayName={}



RE: Clearing an array - Ljg701 - 01-20-2021

(01-19-2021, 10:47 PM)patters Wrote: What is the recommended way to reset an array's contents? Do we have to run a FOR loop from 0 TO UBOUND and clear it element by element, or is there a neater way?
I see that the following doesn't work:

Code:
arrayName={}

You could get the address of the start of the array and then use the MemSet method in the memcopy library to clear it quickly?


RE: Clearing an array - boriel - 01-20-2021

(01-20-2021, 11:30 AM)Ljg701 Wrote:
(01-19-2021, 10:47 PM)patters Wrote: What is the recommended way to reset an array's contents? Do we have to run a FOR loop from 0 TO UBOUND and clear it element by element, or is there a neater way?
I see that the following doesn't work:

Code:
arrayName={}

You could get the address of the start of the array and then use the MemSet method in the memcopy library to clear it quickly?

That's it. Exclamation

A proposal (I think even commented in this forum):
Code:
CLEAR array
or
Code:
CLEAR array, <new value>
but it's not implemented.

Instead you should perhaps use memset:
Code:
#include <memcopy.bas>

DIM myArray(10, 20) as UByte => 11 rows x 21 cols if using array_base 0
..
memset(@myArray(0, 0), 0, 11 * 21)

When dynamics array are implemented you could also ReDIM them... meanwhile...
Finally, remember you can assign (copy) entire arrays (myArray1 = myArray2) provided they are of the same shape and type.


RE: Clearing an array - patters - 01-26-2021

You have my vote for
Code:
CLEAR array



RE: Clearing an array - worcestersource - 04-15-2021

Just the clarify the syntax for copying an array, if mine has dimensions of 27,7, I'd type:

Code:
array1(27,7) = array2(27,7)

Because doing this doesn't appear to copy the data.

Thanks,


Steve

Ah. I figured it out. In the example above, it is literally as Boriel said.

Code:
array1 = array2


What's great with the quick code/compile cycle is that you can use trial and error to figure things out. Smile

Steve


RE: Clearing an array - boriel - 04-16-2021

(04-15-2021, 08:27 PM)worcestersource Wrote: Just the clarify the syntax for copying an array, if mine has dimensions of 27,7, I'd type:

Code:
array1(27,7) = array2(27,7)

Because doing this doesn't appear to copy the data.

Thanks,


Steve

Ah. I figured it out. In the example above, it is literally as Boriel said.

Code:
array1 = array2


What's great with the quick code/compile cycle is that you can use trial and error to figure things out. Smile

Steve

This works. But array2 is always taking your memory. Consider using this within a function so once the function ends, array2 is freed from memory.
Code:
SUB initArray()
    DIM array2(...)
    array1 = array2
END SUB ' Here array2 is freed

Another solution is to use memset (if the array is NOT an array of strings), but these is a bit more complex.

Anyway, this will be available in the near future once I manage to implement DIM for dynamic arrays (as in SinclairBASIC), and REDIM.


RE: Clearing an array - worcestersource - 04-16-2021

Thanks Boriel. I think REDIM will be an elegant solution to what I'm trying to do.

Well worth a few coffees!  Big Grin