Main Menu

UNDim all arrays

Started by kevin, July 02, 2004, 10:20:23 AM

Previous topic - Next topic

kevin

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 :)




PlayBASIC Code: [Select]
 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





Draco9898

so your sending all the stuff in those arrays into KIll me() array and then destroying that by making it Dim kill me(0)?
DualCore Intel Core 2 processor @ 2.3 ghz, Geforce 8600 GT (latest forceware drivers), 2 gigs of ram, WIN XP home edition sp2, FireFox 2.

"You'll no doubt be horrified to discover that PlayBasic is a Programming Language." -Kevin

kevin

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..

Draco9898

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:
DualCore Intel Core 2 processor @ 2.3 ghz, Geforce 8600 GT (latest forceware drivers), 2 gigs of ram, WIN XP home edition sp2, FireFox 2.

"You'll no doubt be horrified to discover that PlayBasic is a Programming Language." -Kevin

kevin

Actually NO it isn't.. the only thing that is moved, is the pointer to where this array is stored.

Draco9898

okay, strange though, a array acessing another array? DBpro could never do this  :lol:
DualCore Intel Core 2 processor @ 2.3 ghz, Geforce 8600 GT (latest forceware drivers), 2 gigs of ram, WIN XP home edition sp2, FireFox 2.

"You'll no doubt be horrified to discover that PlayBasic is a Programming Language." -Kevin

kevin

#6
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.

PlayBASIC Code: [Select]
  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






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

like this..
 

PlayBASIC Code: [Select]
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