Difference between revisions of "List"
Jump to navigation
Jump to search
(Small example) |
m (→Methods) |
||
(12 intermediate revisions by 2 users not shown) | |||
Line 2: | Line 2: | ||
A list is mutable, ordered sequence of values. A list literal is defined by a comma-separated series of values enclosed in square brackets. | A list is mutable, ordered sequence of values. A list literal is defined by a comma-separated series of values enclosed in square brackets. | ||
+ | |||
+ | Example: | ||
<ms> | <ms> | ||
Line 8: | Line 10: | ||
</ms> | </ms> | ||
− | See also [[ | + | == Methods == |
+ | |||
+ | {| class="wikitable" | ||
+ | |- | ||
+ | ! Method !! Description | ||
+ | |- | ||
+ | | len || Returns the length of the list, i.e., the number of items it contains | ||
+ | |- | ||
+ | | indexOf(''item'') || Returns the index of the first occurrence of ''item'' in the list, or <c>null</c> if not found | ||
+ | |- | ||
+ | | push ''item'' || Adds the ''item'' to the end of the list | ||
+ | |- | ||
+ | | remove ''index'' || Removes the element at ''index'' (note: by index, '''not''' by value!) | ||
+ | |- | ||
+ | | pop || Removes the ''last'' element of the list, and returns it | ||
+ | |- | ||
+ | | pull || Removes the ''first'' element of the list, and returns it | ||
+ | |- | ||
+ | | sum || Returns the sum of all numeric values in the list | ||
+ | |- | ||
+ | | sort || Mutates the list by sorting its elements ''in place'' | ||
+ | |- | ||
+ | | shuffle || Mutates the list by re-ordering its elements randomly | ||
+ | |- | ||
+ | | join(''separator''=" ") || Returns a string with all list elements "joined" by a ''separator'' (which defaults to a space) | ||
+ | |- | ||
+ | | insert ''index'', ''value'' || Mutates the list by inserting ''value'' at ''index'' | ||
+ | |- | ||
+ | | replace ''oldValue'', ''newValue'', ''maxCount'' || Mutates the list by replacing occurrences of ''oldValue'' with ''newValue'' at most ''maxCount'', if specified. If ''maxCount'' is omitted it replaces '''all''' occurrences. | ||
+ | |- | ||
+ | | indexes || Returns a list with the indexes of the elements of the list; equivalent to <c>range(0, theList.len-1)</c> | ||
+ | |- | ||
+ | | values || Returns the values of the list (in fact it returns the list itself) | ||
+ | |} | ||
+ | |||
+ | == See also == | ||
+ | * [[listUtil]] - for additional functions on a list | ||
+ | * [[range]] - for building lists of consecutive values | ||
[[Category:Language]] | [[Category:Language]] |
Latest revision as of 18:14, 22 May 2022
list
is one of the core data types in MiniScript.
A list is mutable, ordered sequence of values. A list literal is defined by a comma-separated series of values enclosed in square brackets.
Example:
numbers = [1, 2, 3, 4, 5]
words = ["one", "two", "three", "four", "five"]
Methods
Method | Description |
---|---|
len | Returns the length of the list, i.e., the number of items it contains |
indexOf(item) | Returns the index of the first occurrence of item in the list, or null if not found
|
push item | Adds the item to the end of the list |
remove index | Removes the element at index (note: by index, not by value!) |
pop | Removes the last element of the list, and returns it |
pull | Removes the first element of the list, and returns it |
sum | Returns the sum of all numeric values in the list |
sort | Mutates the list by sorting its elements in place |
shuffle | Mutates the list by re-ordering its elements randomly |
join(separator=" ") | Returns a string with all list elements "joined" by a separator (which defaults to a space) |
insert index, value | Mutates the list by inserting value at index |
replace oldValue, newValue, maxCount | Mutates the list by replacing occurrences of oldValue with newValue at most maxCount, if specified. If maxCount is omitted it replaces all occurrences. |
indexes | Returns a list with the indexes of the elements of the list; equivalent to range(0, theList.len-1)
|
values | Returns the values of the list (in fact it returns the list itself) |
See also
This article is a stub. You can help the MiniScript Wiki by expanding it.