Page 1 of 1

[Done] Need ClearArray() command

Posted: Sun Nov 25, 2018 3:29 am
by Dude
[Edit] Already possible - I didn't RTFM - see later posts. :oops:

There should be a command like ClearArray() to clear all array contents without re-dimming it, and without looping through all items and setting them to null.

I know we can use FreeArray(), but then we need to re-dim, which means dimming it twice for Global arrays:

Code: Select all

Global Dim a$(100) ; Initial first dim. Needed in case user never calls ClearMyData().

Procedure ClearMyData()
  FreeArray(a$())
  Global Dim a$(100) ; Second dim. Shouldn't be needed. Why not just ClearArray() instead of this procedure?
EndProcedure

Procedure SetMyData()
  For n=1 To 100
    a$(n)=Str(Random(100))
  Next
EndProcedure

SetMyData()
Debug a$(1)

ClearMyData() ; Optional in my app, user might not ever need it!
Debug a$(1)

SetMyData()
Debug a$(1)
This is how I'd love the above code to work (more efficient and optimized):

Code: Select all

Global Dim a$(100)

Procedure SetMyData()
  For n=1 To 100
    a$(n)=Str(Random(100))
  Next
EndProcedure

SetMyData()
Debug a$(1)

ClearArray(a$()) ; Doesn't free it, just sets a$(1)="", a$(2)="", etc.
Debug a$(1)

SetMyData()
Debug a$(1)

Re: Need ClearArray() command

Posted: Sun Nov 25, 2018 3:51 am
by skywalk
There is no need for FreeArray() if you are using it again.
Just use Dim a$(100).
That works exactly as you want?

Re: Need ClearArray() command

Posted: Sun Nov 25, 2018 3:54 am
by Dude
skywalk wrote:There is no need for FreeArray() if you are using it again.
Just use Dim a$(100).
That works exactly as you want?
:shock: Skywalk, you legend! :lol: I had no idea it did that. Works great! Thanks!

[Edit] It's even in the manual :oops:
Manual wrote:If Dim is used on an existing array, it will reset its contents to zero.
Sorry for not RTFM. :|

Re: [Done] Need ClearArray() command

Posted: Sun Nov 25, 2018 8:59 am
by #NULL
I wonder why there is no out-of-bounds error. I remember we got such previously. The purifier also doesn't help.

Re: [Done] Need ClearArray() command

Posted: Fri Aug 20, 2021 8:26 pm
by Realizimo
speed test

Code: Select all

#size=40
Dim test(#size)
test_s = 8*(#size+1) ; size integer=8 byte
test_a = @test()     ; address 
#loops=10000000

Starttime = ElapsedMilliseconds()
  For a=1 To #loops
    FillMemory(test_a , test_s)
  Next a
Endtime = ElapsedMilliseconds()
Time = Endtime - Starttime
time$=Str(Time)+"  "

Starttime = ElapsedMilliseconds()
  For a=1 To #loops
    For b=0 To #size
      test(b)=0
    Next b
  Next a
Endtime = ElapsedMilliseconds()
Time = Endtime - Starttime
time$+Str(Time)+"  "

Starttime = ElapsedMilliseconds()
  For a=1 To #loops
    Dim test(#size)
  Next a
Endtime = ElapsedMilliseconds()
Time = Endtime - Starttime
time$+Str(Time)

InputRequester("","",time$)

Re: [Done] Need ClearArray() command

Posted: Fri Oct 22, 2021 1:56 pm
by Realizimo
Is an faster ClearArray needed? sorry but I have big improvement with "FillMemory"

Re: [Done] Need ClearArray() command

Posted: Fri Oct 22, 2021 3:58 pm
by skywalk
That does not handle string arrays and C optimizer performance is what counts.

Re: [Done] Need ClearArray() command

Posted: Fri Oct 22, 2021 4:02 pm
by mk-soft
Important

NOT USE FILLMEMORY WITH STRING ARRAYS