UnderwareDESIGN

PlayBASIC => Resources => Source Codes => Topic started by: kevin on July 02, 2004, 10:20:23 AM

Title: UNDim all arrays
Post by: kevin on July 02, 2004, 10:20:23 AM
Arrays sit in memory as a series of pointers, so in theory if you want to clear a bunch of arrays..  You could loop through the array indexes..  

Heres an example of doing that..   If you have a lot of arrays i guess then building your own undim function would do the job..  It won't work on other array types  though.. So you'd need a version to undim float arrays/strings/ and typed arrays..


There's a bit of bug with UNdim and the redirectable arrays, so i've had to dim arrays with zero.  Which is basically the same thing.. basically :)




[pbcode]

Dim FirstArray(1)
  Dim Table(100)
  Dim Table2(300)
  Dim Table3(300)
  Dim Table4(300)
  Dim Table5(300)
  Dim Table6(300)
  Dim Table7(300)
   Dim LastArray(1)

 ShowStates()
   UnDim_Integer_arrays()
   ShowStates()
   Sync
   WaitKey


Function ShowStates()
   Print "array states"
   MakeArray KillMe()   
  For lp=GetArray(FirstArray()) To GetArray(Lastarray())
 SetArray Killme(),lp
 Print GetArrayStatus(Killme())
 If GetArrayStatus(Killme())=true
    Print "Sioze:"+Str$(GetArrayElements(Killme(),1))
 endif
  Next lp   
EndFunction
 
Function UNDim_Integer_Arrays()
   MakeArray KillMe()   
  For lp=GetArray(FirstArray())+1 To GetArray(Lastarray())-1
 SetArray Killme(),lp
 Dim KillMe(0)
  Next lp   
EndFunction

[/pbcode]

Title: UNDim all arrays
Post by: Draco9898 on July 02, 2004, 10:40:40 AM
so your sending all the stuff in those arrays into KIll me() array and then destroying that by making it Dim kill me(0)?
Title: UNDim all arrays
Post by: kevin on July 02, 2004, 10:43:41 AM
Yep... so Killme() is being redirected to dynamically become those arrays..  it's not very clean, but it's one way to do it for the tmie being..
Title: UNDim all arrays
Post by: Draco9898 on July 02, 2004, 10:47:26 AM
So in theory, the memory you used by an array thats currently being deleted is taking up double the space for a fraction of a second? :lol:
Title: UNDim all arrays
Post by: kevin on July 02, 2004, 10:50:10 AM
Actually NO it isn't.. the only thing that is moved, is the pointer to where this array is stored.
Title: UNDim all arrays
Post by: Draco9898 on July 02, 2004, 11:19:05 AM
okay, strange though, a array acessing another array? DBpro could never do this  :lol:
Title: UNDim all arrays
Post by: kevin on July 02, 2004, 11:27:23 AM
Well it's just different way of doing things.  

One of the best feature (if you ask me) is being able to pass array by reference into functions.. Which is pretty much what the above does (but that's a little more advanced to follow)...

Here's an example of an everyday usage of passing arrays into a function...  The data is not moved, PB merely just informs the local array within the Function/Sub where the array data is stored.  This means that whatever you do to the passed array inside the function, is actually occuring on the arrray that was passed in...

 This means that you an write functions that do certain things, and use them on any array of the same type..


ie.

[pbcode]
 Dim Table1(10)
 Dim Table2(200)
; call the fillarray function and tell it work on the TABLE1() array
 FillArray Table1(),40
; call the fillarray function and tell it work on the TABLE2() array
 FillArray Table2(),12345


etc


Function  FillArray(MyArray(),ThisValue)
For lp =0 to GetArrayElements(MyArray(),1)
   MyArray(lp)=ThisValue
next lp
endfunction


[/pbcode]


Without being able to pass arrays into function, we'd need a custom version of the FillArray function for each Array..

like this..
 

[pbcode]

Function  Fill_Table1(ThisValue)
For lp =0 to GetArrayElements(Table1(),1)
   Table1(lp)=ThisValue
next lp
endfunction

Function  Fill_Table2(ThisValue)
For lp =0 to GetArrayElements(Table2(),1)
   Table2(lp)=ThisValue
next lp
endfunction

[/pbcode]