Arrays.Pop

From Xojo Documentation

Method

Arrays.Pop()

Supported for all project types and targets.

Removes the last element from an array and returns its value.

Usage

result = array.Pop

Part Type Description
result Same as array The element of array that was removed.
array Any valid data type for an array The array from which the last value will be popped.

Notes

The AddRow and Pop methods can be used together to treat an array as a stack.The AddRow method pushes a new element onto the array/stack and the Pop method removes the last element from the array/stack and returns its value.

If array has no values then an OutOfBoundsException is raised.

Sample Code

This example pushes "One", "Two", "Three" onto an array used as a stack and then Pops the values off in reverse order:

Var stack() As String

stack.AddRow("One")
stack.AddRow("Two")
stack.AddRow("Three")

MessageBox(stack.Pop)
MessageBox(stack.Pop)
MessageBox(stack.Pop)

This is a directory-crawling routine that performs a depth-first traversal without using recursion:

Var itemsToInspect(0) As FolderItem
Var item As FolderItem
Var folder As FolderItem
Var ctr As Integer
folder = FolderItem.ShowSelectFolderDialog
If folder.Exists Then
itemsToInspect(0) = folder
While itemsToInspect.LastRowIndex >= 0
item = itemsToInspect.Pop
If item.IsFolder Then
For ctr = 1 To item.Count
itemsToInspect.AddRow(item.ChildAt(ctr))
Next
End If
Wend
End If

See Also

Var statement; Arrays concept for a full list of functions; ParamArray keyword, AddRow method