#35867 - 2003-01-30 04:08 PM
Re: stack-like (push/pop) functions for arrays?
|
Richard H.
Administrator
   
Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
|
Here is a working implementation of pop/push.
As you can see, it relies on the stack ($STACK) being a global variable.
If you run this sample code you will get quote: Popped element: c Popped element: b Popped element: a
; Create an empty stack Global $STACK $STACK=NULL() ; Call wrapper function fnWrapper() Function fnWrapper() ; Push three elements onto the stack ; The first push will implicity create the stack fnPush("STACK","a") fnPush("STACK","b") fnPush("STACK","c") ; Pop all the elements off the stack $sPopped=fnPop("STACK") While(VarType($sPopped)<>0) "Popped element: " + $sPopped ? $sPopped=fnPop("STACK") Loop EndFunction Function fnPush($sArrayName,$vElement) Dim $i ; Local integer $nul=Execute("$$i=VarType($$$sArrayName)") If ($i & 8192) $nul=Execute("$$i=UBound($$$sArrayName)") Else $i=1 EndIf $nul=Execute("Redim Preserve $$$sArrayName[$i+1]") ; Assign pushed value. $nul=Execute("$$$sArrayName[$i]=$$vElement") EndFunction Function fnPop($sArrayName) Dim $i ; Local integer $nul=Execute("$$i=VarType($$$sArrayName)") If ($i & 8192) $nul=Execute("$$i=UBound($$$sArrayName)-1") $nul=Execute("$$fnPop=$$$sArrayName[$i]") ; If there are b If $i $nul=Execute("Redim Preserve $$$sArrayName[$i]") Else $nul=Execute("$$$sArrayName=NULL()") EndIf EndIf EndFunction ; This function does nothing but return an empty value. Function NULL() return EndFunction
|
|
|
Top
|
|
|
|
Moderator: Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart
|
0 registered
and 302 anonymous users online.
|
|
|