Array Methods
The following are the currently-supported array methods.
lines
Defaults to string variables. The supplied string will be split into one string per line in the input argument.
This is equivalent to @split(value '\n')
.
echo @lines($unescape("firstline\nsecondline"))
for line in @lines($unescape("third\nfourth\nfifth"))
echo $line
end
firstline secondline
third
fourth
fifth
split
The supplied string will be split according to a pattern specified as an argument in the method. If no pattern is supplied, then the input will be split by whitespace characters. Useful for splitting simple tabular data.
echo @split("onetwoone" "two")
for data in @split("person, age, some data" ", ")
echo $data
end
for data in @split("person age data")
echo $data
end
one one
person
age
some data
person
age
data
split_at
Defaults to string variables. The supplied string will be split in two pieces, from the index specified in the second argument.
echo @split_at("onetwoone" "3")
echo @split_at("FOOBAR" "3")
#echo @split_at("FOOBAR") #ion: expansion error: split_at: requires an argument
#echo @split_at("FOOBAR" "-1") #ion: expansion error: split_at: requires a valid number as an argument
#echo @split_at("FOOBAR" "8") #ion: expansion error: split_at: value is out of bounds
one twoone
FOO BAR
bytes
Defaults to string variables. Returns an array where the given input string is split by bytes and each byte is displayed as their actual 8-bit number.
echo @bytes("onetwo")
echo @bytes("abc")
111 110 101 116 119 111
97 98 99
chars
Defaults to string variables. Returns an array where the given input string is split by chars.
echo @chars("onetwo")
for char in @chars("foobar")
echo $char
end
o n e t w o
f
o
o
b
a
r
graphemes
Defaults to string variables. Returns an array where the given input string is split by graphemes.
echo @graphemes("onetwo" "3")
for grapheme in @graphemes("foobar")
echo $grapheme
end
o n e t w o
f
o
o
b
a
r
reverse
Defaults to array variables. Returns a reversed copy of the input array.
echo @reverse([1 2 3])
echo @reverse(["a"])
let foo = [1 2 3]
echo @reverse(@foo)
3 2 1
a
3 2 1
subst
Returns the 1. argument if the 1. argument as an array has at least on element.
Returns the 2. argument as the default array if the 1. argument is an empty array.
This methods raises an error
- if no 2 arguments is provided
- if the 1. argument is not an array
- if the 2. argument is not an array
Note: If you want to use this method with an string, use $or method instead or split the string method via @split before using that method.
let empty = []
for number in @subst(@empty [1 2 3])
echo $number
end
1
2
3