To use this website fully, you first need to accept the use of cookies. By agreeing to the use of cookies you consent to the use of functional cookies. For more information read this page.

Official ZPE/YASS documentationList data type

The list data type is one of the most powerful data types in ZPE. The list works similar to an array in other languages but unlike a standard array can be either fixed length or countably infinite.

Defining a list is easy:

YASS
$empty_list = []
$some_list = [1, 2, 3, 4]

//Using TYPO (version 1.9.7+)
declare myList as list = []
  

There are many operations that can be performed on the list but perhaps the most common is simply obtaining an element from the list:

YASS
$some_list = [1, 2, 3, 4]
//This will obtain the third element from the list
$some_element = $some_list[2]
  
YASS
$a = [44, 32, 99]
print([4, 9, 3])
  

Fixed-length arrays

Version 1.6.7 added fixed size lists (arrays). Defining a fixed-length array is also very easy by just defining in the brackets the default value of the array and then telling the compiler how many elements are needed:

YASS
$a = [0] * 4
print($a)
$b = ["Hello world"] * 3
print($b)
  

The previous example will add the number 0 four times to the first list and then add "Hello world" three times to the second list.

The difference between a list and an array (added in version 1.6.7) is that arrays have a fixed size whereas lists can be increased (or decreased) in size very easily. Arrays have the advantage of index existence, that is, one can be assured that such an index does exist.

Other languages use arrays and do not offer a list type and as a result, from an educational perspective, arrays are better suited as a teaching tool.

Negative indices and ranges

ZPE 1.8.6 (Younis) added support for negative indices. These will work backward from 0, e.g. -1 will be the last element within the list and -2 will be the penultimate element.

YASS
$l = [0, 1, 2, 3]
print($l[-1])
  

For the sake of preventing accidental accessing a negative index that does not exist, for example applying -53 here, ZPE sanitises any negtaive index access automatically.

When the INDEX_SAFETY setting is set to on negative indices cannot be used.

ZPE 1.8.6 also added ranges of indices. This allows you to obtain a list of items from the list very quickly.

YASS
$l = [0, 1, 2, 3]
print($l[1:3])
  

Note that the range is inclusive and includes both the first index and the second index when using this range, so the above example would return [1, 2, 3].

Reference functions

The list data type provides several reference functions that allow for ease of use:

to_string () ⇒ string
Converts (casts) the list to a string
put (mixed value) ⇒ list
Puts an element at the end of the list.
push (mixed value) ⇒ list
Adds an element to the start of the list.
get_circular (number index) ⇒ mixed
Gets an element using a circular index, meaning that it will always be index safe. This was formerly called getc in versions prior to ZPE 1.9.10.
sublist (number from, number to) ⇒ list
Gets a sublist of the list.
remove (number index) ⇒ list
Removes an element from a list at a specified index.
length () ⇒ number
Returns the length or size of the list.
Comments

There are no comments on this page.

New comment
Reply
Code previewClose
Feedback 👍
Comments are sent via email to me.