List

Functions for lists created with NewList. See Lists and maps for an introduction.

A list is passed to these functions with empty brackets: ListSize(names()). Functions that return *Result give the address of an element's data, or 0. Most also change the current element.

AddElement ✅

*Result = AddElement(List())

Adds a new element (all zeros or empty strings) after the current element, or at the start if there is no current element. The new element becomes current.

Returns the address of the new element.

InsertElement ✅

*Result = InsertElement(List())

As AddElement, but before the current element.

DeleteElement ✅

*Result = DeleteElement(List() [, Flags])

Deletes the current element. The element before it becomes current. If the first element was deleted there is no current element, unless Flags is 1, which makes the next element current instead.

Returns the address of the new current element, or 0.

ClearList ✅

ClearList(List())

Deletes every element.

ListSize ✅

Result = ListSize(List())

The number of elements.

ListIndex ✅

Index = ListIndex(List())

The position of the current element (0 = the first), or −1 if there is no current element.

ResetList ✅

ResetList(List())

Makes no element current, so the next NextElement goes to the first element.

NextElement ✅

*Result = NextElement(List())

Moves to the next element (or the first, after ResetList).

Returns its address, or 0 at the end of the list. The current element doesn't change then.

PreviousElement ✅

*Result = PreviousElement(List())

Moves to the previous element. Returns 0 at the start.

FirstElement ✅

*Result = FirstElement(List())

Moves to the first element. Returns 0 if the list is empty.

LastElement ✅

*Result = LastElement(List())

Moves to the last element. Returns 0 if the list is empty.

SelectElement ✅

*Result = SelectElement(List(), Position)

Moves to the element at Position (0 = the first). Returns 0 if there's no such element; the current element then doesn't change.

NewList q.s()
AddElement(q()) : q() = "b"
FirstElement(q())
InsertElement(q()) : q() = "a"
LastElement(q())
AddElement(q()) : q() = "c"

OpenConsole()
ResetList(q())
While NextElement(q())
  PrintN(Str(ListIndex(q())) + " " + q())
Wend
SelectElement(q(), 1)
DeleteElement(q())
PrintN(q() + " " + Str(ListSize(q())))

Output:

0 a
1 b
2 c
a 2

CopyList ✅

Result = CopyList(SourceList(), DestinationList())

Makes DestinationList() a copy of SourceList() (the same element type): its old elements are freed, and strings, arrays, lists and maps inside the elements are copied too. The copy's last element is current. Returns non-zero.

CompareList ✅

Result = CompareList(List1(), List2() [, Flags])

Returns non-zero if the two lists (the same element type) have the same number of elements and each pair is equal: strings by their text (#PB_String_NoCase: without case), arrays, lists and maps inside elements by their contents, everything else byte for byte. #PB_Memory_FollowPointers is accepted, but x compares pointers by value (its structures don't record which fields are pointers).

MergeLists ✅

MergeLists(SourceList(), DestinationList() [, Location])

Moves all of SourceList()'s elements into DestinationList() (nothing is copied, so it's fast), leaving the source empty. Location: #PB_List_Last (the default), #PB_List_First, or #PB_List_Before / #PB_List_After the destination's current element (#PB_List_After with no current element adds at the end). The destination's current element stays current.

SplitList ✅

SplitList(SourceList(), DestinationList() [, KeepCurrent])

Moves the current element and those after it to DestinationList(), whose own elements are freed first. With KeepCurrent #True, the current element stays and the ones after it move. The source's current element is then the one before the split (or the kept one); the destination has none. With no current element, nothing moves.

OpenConsole()
NewList a.i() : NewList b.i()
For i = 1 To 6 : AddElement(a()) : a() = i : Next
SelectElement(a(), 3)
SplitList(a(), b())
Print("a:") : ForEach a() : Print(" " + Str(a())) : Next : PrintN("")
Print("b:") : ForEach b() : Print(" " + Str(b())) : Next : PrintN("")
FirstElement(a())
MergeLists(b(), a(), #PB_List_After)
Print("merged:") : ForEach a() : Print(" " + Str(a())) : Next
PrintN(" (b has " + Str(ListSize(b())) + ")")

Output:

a: 1 2 3
b: 4 5 6
merged: 1 4 5 6 2 3 (b has 0)

MoveElement ✅

MoveElement(List(), Location [, *RelativeElement])

Moves the current element: #PB_List_First, #PB_List_Last, or #PB_List_Before / #PB_List_After the element at *RelativeElement (an element's address: @List()). It stays the current element.

SwapElements ✅

SwapElements(List(), *FirstElement, *SecondElement)

Swaps two elements' places, given their addresses (@List()). The current element stays the same element.

ChangeCurrentElement ✅

ChangeCurrentElement(List(), *Element)

Makes the element at *Element (an address from @List()) current.

PushListPosition, PopListPosition ✅

PushListPosition(List())
*Element = PopListPosition(List())

PushListPosition remembers which element is current (or that none is); PopListPosition makes it current again, the last remembered first. Useful in a procedure that goes through a list its caller is also going through. A remembered element must not be deleted in between.

OpenConsole()
NewList n.s()
For i = 1 To 5 : AddElement(n()) : n() = "n" + Str(i) : Next
SelectElement(n(), 1)
PushListPosition(n())
ForEach n()
  If n() = "n4" : *four = @n() : EndIf
Next
PopListPosition(n())
PrintN("current again: " + n())
MoveElement(n(), #PB_List_After, *four)
SwapElements(n(), FirstElement(n()), LastElement(n()))
ForEach n() : Print(n() + " ") : Next
PrintN("")

Output:

current again: n2
n5 n3 n4 n2 n1 

FreeList ✅

FreeList(List())

Frees all the elements. (In PureBasic the list can't be used afterwards without NewList; in x it is an empty list.)

Sorting lists is in Sort.