Lists are a powerful part of PRPL that lets you store multiple objects in one variable of type list. The objects in a list are ordered and indexed from 0, that means the first object has position 0, second object has position 1, and so on.
#Create an empty list and then add values CreateList ->list <-list "a" AppendToList <-list "b" AppendToList <-list "c" AppendToList Trace(<-list) #["a", "b", "c"] #Or use split to save the effort Split("a,b,c" ",") ->list2 Trace(<-list2) #["a", "b", "c"]
However, the main focus of this article is to explain how lists behave when multiple variables contain the same list, when you put lists into other lists as elements, or when you pass lists into functions.
If you are familiar with programming in C-family languages, you have probably heard the term pass by value vs pass by reference. For those of you that haven't, I will try to explain this concept as simply as possible.
Let me start by asking a seemingly simple question. Are the lists list
and list2
from the How to create a list example same? They certainly contain same elements, however they are not same. Why not? Because they are different lists, that just happen to contain the same elements (in the same order).
The best way to test this is to modify “list” and verify that “list2” remains unchanged:
<-list "d" AppendToList Trace(<-list) #["a", "b", "c", "d"] Trace(<-list2) #["a", "b", "c"]
In this example, the variables list
and list2
each contained a different list, so when the list stored in the list
variable was modified, the other list remained unchanged.
So, the question becomes: Can 2 lists ever be the same? No, because then it would be just one and the same list. However, 2 different variables can contain the same list. For example:
<-list ->list3 <-list "e" AppendToList Trace(<-list) #["a", "b", "c", "d", "e"] Trace(<-list3) #["a", "b", "c", "d", "e"]
In this example, both variables list
and list3
contain one and the same list inside them, so when this list was changed using the list
varaible, the change was visible regardless of which variable we used to print the list.
At this point I would use a picture but I'm not sure I have the permision to upload pictures, so I'll get to it later today hopefully