Page 1 of 1 1
Topic Options
#35857 - 2003-01-30 12:09 AM stack-like (push/pop) functions for arrays?
jnoble Offline
Lurker

Registered: 2003-01-30
Posts: 2
I'm newish to Kixtart, but a long-time programmer with a recent emphasis on Perl.

I'm curious if there's any handy way to treat an array like a stack. That is, push a new element onto the front, or pop the first element off the front. Or delete an element from the middle of an array.

As far as I can see in the manual there isn't any such support. Therefore, I end up having to wrestle with manually renumbering the array indexes if I want to add something to the front (shift "1" to "2", "2" to "3", etc. etc. Gack!), and similar if I want to pop or delete. Lots and lots of manual work.

In many programming languages, these "stack-like" operations are part of an array implementation. But I can't find any reference to similar concepts with Kixstart. Perhaps it's just named something I wouldn't have guessed (?).

Any ideas?

- jnoble

Top
#35858 - 2003-01-30 01:58 AM Re: stack-like (push/pop) functions for arrays?
Bryce Offline
KiX Supporter
*****

Registered: 2000-02-29
Posts: 3167
Loc: Houston TX
hmmmm sounds like a good idea fo a UDF!!

and who knows... it just might be added to a future release of kix.....

[ 30. January 2003, 02:34: Message edited by: Bryce ]

Top
#35859 - 2003-01-30 02:02 AM Re: stack-like (push/pop) functions for arrays?
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
I have written a UDF that performs like a hash and there has been discussion in the past of using string stacks as arrays which could be programmed to behave in a Perl like fashion. Try searching the board for stack(s).
_________________________
Home page: http://www.kixhelp.com/hb/

Top
#35860 - 2003-01-30 02:25 AM Re: stack-like (push/pop) functions for arrays?
Bryce Offline
KiX Supporter
*****

Registered: 2000-02-29
Posts: 3167
Loc: Houston TX
here are some quick UDF's......



function Push($item$array)
    dim $i
    redim preserve $array[ubound($array) + 1]
    for $i = ubound($arrayto 1 step -1
        $array[$i] = $array[$i-1]
    next
    $array[0] = $item
    $push = $array
endfunction

function pop($array)
    dim $i
    dim $temparray[ubound($array)-1]
    for $i = 0 to ubound($temparray)
        $temparray[$i] = $array[$i+1]
    next
    $pop = $temparray
endfunction


Top
#35861 - 2003-01-30 09:15 AM Re: stack-like (push/pop) functions for arrays?
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
Bryce me old china,

The "pop" UDF won't work, and in fact it can't work in KiXtart because we don't have "pass by reference" [Frown]

Although you are popping off the top of the stack, you are not returning the popped off element - it is lost. "Pop" should pop the element off the top of the stack and return it.
The only way to implement it in a UDF is to use the kludgy work-around of making the array a global variable, and then passing the name and using Execute() to manipulate it.

As an aside, if you are using just "pop" and "push" to manipulate the stack it doesn't matter which end is the "top". This means that you can save the overhead of copying, and just add/remove the end element of the array.

Top
#35862 - 2003-01-30 02:14 PM Re: stack-like (push/pop) functions for arrays?
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
mmm...
quote:
The only way to implement it in a UDF is to use the kludgy work-around of making the array a global variable, and then passing the name and using Execute() to manipulate it.

not buying this one...
not sure what you mean about can't work...
_________________________
!

download KiXnet

Top
#35863 - 2003-01-30 02:38 PM Re: stack-like (push/pop) functions for arrays?
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
It's quite simple.

To "pop" something off a stack you need to do two things.
1) Reduce the size of the stack by one element.
2) Return the element that was on the top of the stack.

Ok, so "2)" is easy - set the value of the function to the value of the elemnt that was on the top of the stack. The return value of the function is the "popped" element.

How do you do "1)" then? Well, you can't. The array is passed by value, not reference, so it cannot be modified.

As I said in the previous post, the only way to do it is to make your array a global variable, pass the variable name and use Execute() to modify it.

Top
#35864 - 2003-01-30 02:43 PM Re: stack-like (push/pop) functions for arrays?
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
To clarify, when I say "it won't work", I mean that it won't work as a "pop" function.

It works perfectly well as a "return a new array which is a copy of the passed array with the first element removed" function.

Top
#35865 - 2003-01-30 02:46 PM Re: stack-like (push/pop) functions for arrays?
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
does not need to be global... shouldn't need to anyway.

doesn't the dimmed var get to it's subs.
_________________________
!

download KiXnet

Top
#35866 - 2003-01-30 03:25 PM Re: stack-like (push/pop) functions for arrays?
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
The POP function returns the popped off values and reconfigures the array without the popped off element.

There is no good way to accomplish this in Kix The function would have to return the popped value and redim the input array. That means that the function has to manipulate the GLOBAL array by name using EXECUTE like I do in my HASH UDFs.
_________________________
Home page: http://www.kixhelp.com/hb/

Top
#35867 - 2003-01-30 04:08 PM Re: stack-like (push/pop) functions for arrays?
Richard H. Administrator Offline
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
#35868 - 2003-01-30 04:19 PM Re: stack-like (push/pop) functions for arrays?
jnoble Offline
Lurker

Registered: 2003-01-30
Posts: 2
Richard -- Thanks! I'll test it!

Would you also happen to have "delete" for an array element? This isn't a "stack" operation, since it's reaching in to the middle of the array, but similarly it would need to re-shuffle the remaining elements to fill the "hole" and the overall length of the array would have to be reduced by one.

Great folks on this board -- fun discussion!

jnoble

Top
#35869 - 2003-01-30 04:34 PM Re: stack-like (push/pop) functions for arrays?
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
Your welcome.

I don't have a "delete" UDF to hand (and I'm just off home), but I'm sure one will be along in a moment...

A couple of warnings if you are going to use these UDFs:
  • To keep it simple and fast, the pop/push is using the "high" end of the array as the top of the stack, not array subscript 0. Use Ubound() if you want to "peek" at the top of the stack.
  • To identify an empty stack the array is destroyed when the last element is popped. Be careful about referencing the stack especially in an array context - if you use an array reference on a depleted stack you will likely get a runtime error. This is the reason for the VarType() checks in the UDFs.

Top
Page 1 of 1 1


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 302 anonymous users online.
Newest Members
Sir_Barrington, batdk82, StuTheCoder, M_Moore, BeeEm
17886 Registered Users

Generated in 0.062 seconds in which 0.024 seconds were spent on a total of 12 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org