Clear/erase arrays/structures

Just starting out? Need help? Post your questions and find answers here.
User avatar
Thumper
User
User
Posts: 97
Joined: Sat Feb 01, 2014 2:16 am
Location: Alabama USA

Clear/erase arrays/structures

Post by Thumper »

prescript: I re-created this post here as this accidentally ended up in the Windows section, probably due to having many tabs open.

Before I ask, it has been fun as it seems that a few others are on the same path... recent questions have been relevant toward where I am at in my learning PureBasic. Actually, I have a new program well underway, my pre-compiler is already doing its job and things are progressing nicely.

Okay, the question here is what is the best way to "reset" an array and/or structure ?

For arrays, I'm just doing a for i = 1 to arraySize ( myArray ( ) ) and for structures, define a clearData.myStructure and overwriting my data myData = clearData.

Are there any concerns if I have arrays with structures in my structures ? Stuff like record(1)\filelist[1]\filename ?

I'll leave it that. The technique seems to work. Below is a simplified sample of what I've using just in case anyone spots anything I'm doing that is not advisable.

Code: Select all

     structure _playlistrecords
          filename.s
          description.s
          duration.i
     endStructure

     structure _record
          description.s
          playlistrecords.i
          playlist._playlistrecords [ 101 ]
     endStructure

     define s.s, i.i, j.i
     define dim record._record ( 500 )

     record(1)\description = "First record"
     record(1)\playlistrecords = 3
     record(1)\playlist[1]\filename = "First record, First item"
     record(1)\playlist[2]\filename = "First record, Second item"
     record(1)\playlist[3]\filename = "First record, Third item"

     record(2)\description = "Second record"
     record(2)\playlistrecords = 2
     record(2)\playlist[1]\filename = "Second record, First item"
     record(2)\playlist[2]\filename = "Second record, Second item"

     for i = 1 to 2
          s = s + str(i) + ":" + record(i)\description + chr(13) + chr(10)
          for j = 1 to record(i)\playlistrecords
               s = s + "   " + str(i) + "," + str(j) + ":" + record(i)\playlist[j]\filename + chr(13) + chr(10)
          next
          s = s + chr(13) + chr(10)
     next

     s = s + "----- CLEARING -----" + chr(13) + chr(10)

     define clearRecord._record

     for i = 0 to arraySize ( record ( ) )
          record(i) = clearRecord
     next

     for i = 1 to 2
          s = s + str(i) + ":" + record(i)\description + chr(13) + chr(10)
          for j = 1 to record(i)\playlistrecords
               s = s + "   " + str(i) + "," + str(j) + ":" + record(i)\playlist[j]\filename + chr(13) + chr(10)
          next
          s = s + chr(13) + chr(10)
     next

     messageRequester ( "TEST", s )

;    messageBox results captured using ^c
;
;    ---------------------------
;    TEST
;    ---------------------------
;    1:First record
;       1,1:First record, First item
;       1,2:First record, Second item
;       1,3:First record, Third item
;
;    2:Second record
;      2,1:Second record, First item
;      2,2:Second record, Second item
;
;    ----- CLEARING -----
;    1:
;
;    2:
;
;
;    ---------------------------
;    OK
;    ---------------------------
I enter the PureBasic world, targeting both Mac and Windows, by way of PowerBasic, Win32API, EZGUI back to CP/M via various BASIC dialects, xBase and other languages as needed.
Side project: http://www.thecomputerarchive.com
User avatar
idle
Always Here
Always Here
Posts: 5835
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Clear/erase arrays/structures

Post by idle »

Doesn't FreeArray() work for you?
User avatar
STARGÅTE
Addict
Addict
Posts: 2226
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: Clear/erase arrays/structures

Post by STARGÅTE »

ResetStructure() - clears a structured memory area and initialize it to be ready to use.
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
User avatar
Thumper
User
User
Posts: 97
Joined: Sat Feb 01, 2014 2:16 am
Location: Alabama USA

Re: Clear/erase arrays/structures

Post by Thumper »

[I have had to type this several times... the forum asks me to re-login, even after only a few minutes...]

Okay, I had not considered that as something about it does not seem quite right. Perhaps some historical limitation lingers in my mind.

Being that I need to dim the array first for the compiler, I can follow it up with a procedure to dim it again.

Code: Select all

     global dim record._record ( 500 )

     procedure reset_record ( )
          protected.i resetRecords = arraySize ( record ( ) )
          freeArray ( record ( ) )
          global dim record._record ( resetRecords )
     endProcedure
I cannot see a cleaner way to do this but it will only be for 5 different global arrays.

For non-globals, this will not be needed very often and I'll just have to hand-write the arraySize, freeArray and dim.

Meanwhile, I'm enjoying the capabilities of the structures where they can be nested with variable length strings and arrays.

Cheers
I enter the PureBasic world, targeting both Mac and Windows, by way of PowerBasic, Win32API, EZGUI back to CP/M via various BASIC dialects, xBase and other languages as needed.
Side project: http://www.thecomputerarchive.com
User avatar
Thumper
User
User
Posts: 97
Joined: Sat Feb 01, 2014 2:16 am
Location: Alabama USA

Re: Clear/erase arrays/structures

Post by Thumper »

STARGÅTE wrote: Sun Dec 22, 2024 10:03 pm ResetStructure() - clears a structured memory area and initialize it to be ready to use.
It was to find that one! Your link wasn't working (could be my ISP) but it gave me the keyword "compiler functions".

ResetStructure looks good for the non-array variables. Arrays looks to be better wiped out by the free and (re) dim.

Thanks for the inputs.
I enter the PureBasic world, targeting both Mac and Windows, by way of PowerBasic, Win32API, EZGUI back to CP/M via various BASIC dialects, xBase and other languages as needed.
Side project: http://www.thecomputerarchive.com
User avatar
robertfern
User
User
Posts: 38
Joined: Fri Feb 02, 2018 10:33 pm
Location: New Jersey

Re: Clear/erase arrays/structures

Post by robertfern »

If by reset, you mean set all values back to zero, you can use FillMemory like so...
mylen.l = 100
Dim MyArray.l(mylen)
...
; use array
...
; reset array
FillMemory(@ MyArray(), mylen -1, 0, #PB_Long)
Mac OSX Ventura & Windows 10, PB 6.12
User avatar
mk-soft
Always Here
Always Here
Posts: 6202
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Clear/erase arrays/structures

Post by mk-soft »

robertfern wrote: Mon Dec 23, 2024 4:03 pm If by reset, you mean set all values back to zero, you can use FillMemory like so...
mylen.l = 100
Dim MyArray.l(mylen)
...
; use array
...
; reset array
FillMemory(@ MyArray(), mylen -1, 0, #PB_Long)
No,
that's not the right way. If strings or array, list map are present, this inevitably leads to memory leaks.
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