[Done] Need ClearArray() command

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

[Done] Need ClearArray() command

Post 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)
Last edited by Dude on Sun Nov 25, 2018 10:11 am, edited 2 times in total.
User avatar
skywalk
Addict
Addict
Posts: 3972
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Need ClearArray() command

Post 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?
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: Need ClearArray() command

Post 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. :|
#NULL
Addict
Addict
Posts: 1440
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

Re: [Done] Need ClearArray() command

Post by #NULL »

I wonder why there is no out-of-bounds error. I remember we got such previously. The purifier also doesn't help.
Realizimo
User
User
Posts: 64
Joined: Sun Nov 25, 2012 5:27 pm
Location: Sweden

Re: [Done] Need ClearArray() command

Post 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$)
Realizimo
User
User
Posts: 64
Joined: Sun Nov 25, 2012 5:27 pm
Location: Sweden

Re: [Done] Need ClearArray() command

Post by Realizimo »

Is an faster ClearArray needed? sorry but I have big improvement with "FillMemory"
User avatar
skywalk
Addict
Addict
Posts: 3972
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: [Done] Need ClearArray() command

Post by skywalk »

That does not handle string arrays and C optimizer performance is what counts.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: [Done] Need ClearArray() command

Post by mk-soft »

Important

NOT USE FILLMEMORY WITH STRING ARRAYS
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Post Reply