Array methods

.length()

Return the number of elements contained in the target array.
1
array.length() => Integer
2
3
// example
4
do val = ["Batman", "Robin", "Superman"]
5
do val.length() // 3
Copied!

.push(*)

Add an element at the end of an array.
1
array.push(val) => void
2
3
// example
4
do val = ["Batman", "Superman"]
5
do val.push("Robin") // ["Batman", "Superman", "Robin"]
Copied!

.pop()

Remove the last element of an array.
1
array.pop() => void
2
3
// example
4
do val = ["Batman", "Robin", "Superman"]
5
do val.pop() // ["Batman", "Robin"]
Copied!

.reverse()

Create a new Array with all elements reversed
1
do vec = [3, 2, 1]
2
3
do new_vec = vec.reverse()
4
say new_vec.to_string() // [1, 2, 3]
Copied!

.append()

Add the elements of the array to the initial array
1
do vec = [1, 2, 3]
2
do vec2 = [4, 5, 6]
3
4
do append_vec = vec.append(vec2)
5
say append_vec.to_string() // [1, 2, 3, 4, 5, 6]
Copied!

.insert_at(Integer, *)

Add an element at position n of an array (shifting the position of all the following elements).
1
array.insert_at(n, val) => void
2
3
// example
4
do val = ["Batman", "Superman"]
5
do val.insert_at(1, "Robin") // ["Batman", "Robin", "Superman"]
Copied!

.remove_at(Integer)

Remove the nth element of an array (unshifting the position of all the following elements).
1
array.remove_at(n) => void
2
3
// example
4
do val = ["Batman", "Robin", "Superman"]
5
do val.remove_at(1) // ["Batman", "Superman"]
Copied!

.find(*)

Returns a new array with all the values found in the original array matching the given value.
1
array.find(x) => Array
2
3
// example
4
do val = ["Batman", "Robin", "Superman", "Batman"]
5
do val.find("Ironman") // []
6
do val.find("Robin") // ["Robin"]
7
do val.find("Batman") // ["Batman", "Batman"]
Copied!
You can find more info about the particular regex syntax used in the *_regex methods on this link.

.to_uppercase()

Return the same string in all uppercase characters.
1
string.to_uppercase() => String
2
3
// example
4
do val = "Where is Brian?"
5
do val.to_uppercase() // "WHERE IS BRIAN?"
Copied!

.to_lowercase()

Return the same string in all lowercase characters.
1
string.to_lowercase() => String
2
3
// example
4
do val = "Where is Brian?"
5
do val.to_lowercase() // "where is brian?"
Copied!

.length()

Return the length of the target string.
1
string.length() => Integer
2
3
// example
4
do val = "Where is Brian?"
5
do val.length() // 15
Copied!

.contains(String), .contains_regex(String)

Return whether the string contains another string or expression.
1
haystack.contains(needle) => Boolean
2
haystack.contains_regex(needle) => Boolean
3
4
// example
5
do val = "Where is Brian?"
6
// does it contain any "r"?
7
do val.contains("r") // true
8
// does it contain the word "where"?
9
do val.contains("where") // false => no, because it is case sensitive
10
// does it contain any number?
11
do val.contains_regex("[0-9]") // true
Copied!

.starts_with(String), .starts_with_regex(String)

Return whether a string starts with another string or expression.
1
haystack.starts_with(needle) => Boolean
2
haystack.starts_with_regex(needle) => Boolean
3
4
// example
5
do val = "Where is Brian?"
6
// does it start with "r"?
7
do val.starts_with("r") // false
8
// does it start with any uppercase letter?
9
do val.starts_with_regex("[A-Z]") // true
Copied!

.ends_with(String), .ends_with_regex(String)

Return whether a string ends with another string or expression.
1
haystack.ends_with(needle) => Boolean
2
haystack.ends_with_regex(needle) => Boolean
3
4
// example
5
do val = "Where is Brian?"
6
// does it end with "r"?
7
do val.ends_with("r") // false
8
// does it end with any uppercase letter?
9
do val.ends_with_regex("[A-Z]") // false
Copied!

.match(String), .match_regex(String)

Return all the matches of the string or expression in the target string, or Null if none are found.
1
haystack.match(needle) => Array[String]
2
haystack.match_regex(needle) => Array[String]
3
4
// example
5
do val = "Where is Brian?"
6
// does it match with "r"?
7
do val.match("r") // ["r", "r"] => yes, twice!
8
// does it match with any uppercase letter?
9
do val.match_regex("[A-Z]") // ["W", "B"] => yes, and these are the letters!
Copied!
About _regex methods:
The \ (backslash) character has a special meaning. For technical reasons, in all strings, it must be properly escaped, by convention by adding another \ in front of itself, to avoid being interpreted as a special character. For example, if you mean to write the exact string "\n" you must in fact write \\n, otherwise \n will be interpreted as a line break.
This Python documentation explains why it especially matters in Regex syntax to escape backslashes: https://docs.python.org/2/howto/regex.html#the-backslash-plague
We 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.

.init(size)

Create a new array of size n
1
do arr = [].init(3) // [null, null, null]
Copied!

.slice(start, end)

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.
1
do x = ["a", "b", "c", "d", "e"].slice(2, 4)
2
say "{{x}}" // c, d
3
4
do x = ["a", "b", "c", "d", "e"].slice(2)
5
say "{{x}}" // c, d, e
6
7
do x = ["a", "b", "c", "d", "e"].slice(-4)
8
say "{{x}}" // b, c, d, e
9
10
do x = ["a", "b", "c", "d", "e"].slice(-4, 3)
11
say "{{x}}" // b, c
12
13
do x = ["a", "b", "c", "d", "e"].slice(-2, 1)
14
say "{{x}}" // Error
15
16
do x = ["a", "b", "c", "d", "e"].slice(2, 1)
17
say "{{x}}" // Error
18
Copied!

.map(fn), .filter(fn), .reduce(acc, fn)

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:
1
// create a new array where each original item is multiplied by 2
2
do newArray = [1, 2, 3, 4].map((item, index) {
3
return x * 2
4
}) // newArray = [2, 4, 6, 8]
5
6
// create a new array containing even numbers from the original array
7
do newArray = [1, 2, 3, 4].filter((item, index) {
8
return x % 2 == 0
9
}) // newArray = [2, 4]
10
11
// create a new value by adding all the elements together
12
do sum = [1, 2, 3, 4].reduce(0, (acc, val, index) {
13
do acc = acc + val
14
return acc
15
}) // sum = 1 + 2 + 3 + 4 = 10
Copied!

.flatten()

Convert an array of arrays to an array containing all elements of the 1st level arrays.
1
do [[1, 2], [3, 4]].flatten() // [1, 2, 3, 4]
2
3
// if a 1st-level element is not an array, it will be kept as is
4
do [[1, 2], "something", [3, 4]].flatten() // [1, 2, "something", 3, 4]
Copied!