Arrays.Pop

From Xojo Documentation

Revision as of 13:49, 30 April 2020 by PLefebvre (talk | contribs) (Sample Code: Use For Each in item.Children)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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) // Three
MessageBox(stack.Pop) // Two
MessageBox(stack.Pop) // One

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
folder = FolderItem.ShowSelectFolderDialog
If folder.Exists Then
itemsToInspect(0) = folder
While itemsToInspect.LastRowIndex >= 0
item = itemsToInspect.Pop
If item.IsFolder Then
For Each file As FolderItem In item.Children
itemsToInspect.AddRow(file)
Next
End If
Wend
End If

See Also

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