Return the number of elements contained in the target array.
Add an element at the end of an array.
Remove the last element of an array.
Create a new Array with all elements reversed
Add the elements of the array to the initial array
Add an element at position n of an array (shifting the position of all the following elements).
Remove the nth element of an array (unshifting the position of all the following elements).
Returns a new array with all the values found in the original array matching the given value.
You can find more info about the particular regex syntax used in the *_regex methods on .
Return the same string in all uppercase characters.
Return the same string in all lowercase characters.
Return the length of the target string.
Return whether the string contains another string or expression.
Return whether a string starts with another string or expression.
Return whether a string ends with another string or expression.
Return all the matches of the string or expression in the target string, or Null if none are found.
Create a new array of size n
Return a new array with all items between start and end. Some rules apply:
If end is not specified, all the items after start are returned.
When specified, end must be ≥ start.
If any of the parameters is < 0, the count is made from the end of the array.
These methods are useful ways to construct a new arrays from existing arrays. They are inspired from similar methods in other languages and follow the same general syntax.
Here are some examples of what these methods can help you achieve:
Convert an array of arrays to an array containing all elements of the 1st level arrays.
array.length() => Integer
// example
do val = ["Batman", "Robin", "Superman"]
do val.length() // 3We follow this nomenclature for CSML Regex handling, so a single Regex backslash must be written as a "\\" string, and an escaped backslash (that behaves as a literal "\" string character) must in fact be escaped twice, once for being in a string, and once for being in a Regex: you have to write "\\\\" to result in the Regex syntax \\which in turn matches the literal "\" string.
In a future release of CSML we might introduce a "raw string" method to bypass this limitation.
array.push(val) => void
// example
do val = ["Batman", "Superman"]
do val.push("Robin") // ["Batman", "Superman", "Robin"]array.pop() => void
// example
do val = ["Batman", "Robin", "Superman"]
do val.pop() // ["Batman", "Robin"]do vec = [3, 2, 1]
do new_vec = vec.reverse()
say new_vec.to_string() // [1, 2, 3] do vec = [1, 2, 3]
do vec2 = [4, 5, 6]
do append_vec = vec.append(vec2)
say append_vec.to_string() // [1, 2, 3, 4, 5, 6]array.insert_at(n, val) => void
// example
do val = ["Batman", "Superman"]
do val.insert_at(1, "Robin") // ["Batman", "Robin", "Superman"]array.remove_at(n) => void
// example
do val = ["Batman", "Robin", "Superman"]
do val.remove_at(1) // ["Batman", "Superman"]array.find(x) => Array
// example
do val = ["Batman", "Robin", "Superman", "Batman"]
do val.find("Ironman") // []
do val.find("Robin") // ["Robin"]
do val.find("Batman") // ["Batman", "Batman"]string.to_uppercase() => String
// example
do val = "Where is Brian?"
do val.to_uppercase() // "WHERE IS BRIAN?"string.to_lowercase() => String
// example
do val = "Where is Brian?"
do val.to_lowercase() // "where is brian?"string.length() => Integer
// example
do val = "Where is Brian?"
do val.length() // 15haystack.contains(needle) => Boolean
haystack.contains_regex(needle) => Boolean
// example
do val = "Where is Brian?"
// does it contain any "r"?
do val.contains("r") // true
// does it contain the word "where"?
do val.contains("where") // false => no, because it is case sensitive
// does it contain any number?
do val.contains_regex("[0-9]") // truehaystack.starts_with(needle) => Boolean
haystack.starts_with_regex(needle) => Boolean
// example
do val = "Where is Brian?"
// does it start with "r"?
do val.starts_with("r") // false
// does it start with any uppercase letter?
do val.starts_with_regex("[A-Z]") // truehaystack.ends_with(needle) => Boolean
haystack.ends_with_regex(needle) => Boolean
// example
do val = "Where is Brian?"
// does it end with "r"?
do val.ends_with("r") // false
// does it end with any uppercase letter?
do val.ends_with_regex("[A-Z]") // falsehaystack.match(needle) => Array[String]
haystack.match_regex(needle) => Array[String]
// example
do val = "Where is Brian?"
// does it match with "r"?
do val.match("r") // ["r", "r"] => yes, twice!
// does it match with any uppercase letter?
do val.match_regex("[A-Z]") // ["W", "B"] => yes, and these are the letters!do arr = [].init(3) // [null, null, null]do x = ["a", "b", "c", "d", "e"].slice(2, 4)
say "{{x}}" // c, d
do x = ["a", "b", "c", "d", "e"].slice(2)
say "{{x}}" // c, d, e
do x = ["a", "b", "c", "d", "e"].slice(-4)
say "{{x}}" // b, c, d, e
do x = ["a", "b", "c", "d", "e"].slice(-4, 3)
say "{{x}}" // b, c
do x = ["a", "b", "c", "d", "e"].slice(-2, 1)
say "{{x}}" // Error
do x = ["a", "b", "c", "d", "e"].slice(2, 1)
say "{{x}}" // Error
// create a new array where each original item is multiplied by 2
do newArray = [1, 2, 3, 4].map((item, index) {
return x * 2
}) // newArray = [2, 4, 6, 8]
// create a new array containing even numbers from the original array
do newArray = [1, 2, 3, 4].filter((item, index) {
return x % 2 == 0
}) // newArray = [2, 4]
// create a new value by adding all the elements together
do sum = [1, 2, 3, 4].reduce(0, (acc, val, index) {
do acc = acc + val
return acc
}) // sum = 1 + 2 + 3 + 4 = 10do [[1, 2], [3, 4]].flatten() // [1, 2, 3, 4]
// if a 1st-level element is not an array, it will be kept as is
do [[1, 2], "something", [3, 4]].flatten() // [1, 2, "something", 3, 4]