Data Handling¶
There are four key types of data in GreyScript:
Info
There are also objects, but we will deal with these later in the API.
Strings¶
Strings in GreyScript should be enclosed in double quotes "like this"
. If you wish to use quotation marks within a string itself, you must double them.
Use proper quotes
print("check this out") // "check this out"
print('this fails') // error: you must use " "
print("print(""hello"")") // outputs 'print("hello")'
Numbers can be concatenated with a string (the number will be converted to a string)
String + number
print("1 plus " + 3 + " = 4") // "1 plus 3 = 4"
Strings may be multiplied or divided by a number
String multiplication and division
"test" * 3 // "testtesttest"
"test" * 3.5 // "testtestte"
"test" / 4 // "t"
String Operators¶
Info
For the following operator examples, the variables are defined as below:
s = "apple"
t = "orange"
n = 3
m = 5
Operator | Operation | Returns | Outcome |
---|---|---|---|
s + t | concatenation | string formed by concatenating t onto s |
appleorange |
s + m | numbers can be concatenated onto a string | apple5 |
|
s * n | replication | s repeated n times |
appleappleapple |
s * 2.5 | fractions of s for float 2.5 |
appleappleap |
|
s / n | division | equivalent to s * (1 / n ) |
a |
t[m] | index | character m of t |
e |
t[-1] | supports negative index | e |
|
s[:n] | left slice | substring of s from index 0 to n |
app |
s[n:] | right slice | substring of s from index n to the end |
le |
s[n:m] | slice | substring of s from char n up to m |
le |
s == t | equals | 1 if s equals t (case-sensitive) or else 0 |
0 |
s != t | not equals | 1 if s is not equal to t , or else 0 |
1 |
s > t | greater than | 1 if s.len is greater than t , or else 0 |
0 |
s >= t | greater than or equal | 1 if s is greater than or equal to t , else 0 |
0 |
s < t | less than | 1 if s.len is less than t , else 0 |
1 |
s <= t | less than or equal | 1 if s.len is less than or equal to t , else 0 |
1 |
String Functions¶
Reference
For the following functions, example snippets will use the variables defined below:
title = "Saving Private Ryan"
remove(substr)¶
Remove the first occurrence of a substring from a string
Returns the string without the first occurrence of substr (string
)
Example Usage
title.remove("nope") // "Saving Private Ryan" (unaltered)
title.remove("i") // "Savng Private Ryan" (removes first instance of "i" only)
title.remove("av") // "Sing Private Ryan"
title.remove("av").remove(" Ryan") + "ly!" // "Sing Privately!"
Remember: strings are immutable
All string functions return a new string, leaving the original string
unaffected by any mutations. If we wanted to modify title
, we would need
to redeclare it.
title.remove("av")
print(title) // "Saving Private Ryan"
title = title.remove("av")
print(title) // "Sing Private Ryan"
hasIndex(index)¶
Does the index (int
) exist?
Returns 1
(true) or 0
(false)
Example Usage
title.hasIndex(0) // 1
title.hasIndex(11) // 1
title.hasIndex(22) // 0
indexOf(substr, begin=null)¶
Get the index of the first substring (string
) within string. Optionally searches after begin (int
index)
Returns index (int
) or null
(substring not found)
Example Usage
title.indexOf("R") // 15
title.indexOf("av") // 1
title.indexOf("Matt Damon") // null
title.indexOf("i") // 3
title.indexOf("i", 5) // 9
i = title.indexOf("P")
if i then
print(title[i:]) //prints "Private Ryan"
end if
lastIndexOf(substr)¶
Get the index of the last substring (substr
) within string. Takes a string
.
Returns index (int
) or null
(substring not found)
Example Usage
title.lastIndexOf("a") // 17
NEW
This function only currently exists for strings, but list.lastIndexOf and map.lastIndexOf will be coming soon.
slice(str, start, end)¶
Takes a string
(or list
), start (int
) and optional end (int
)
Returns string
str from index start to index end
Example Usage
slice(title, 0, 6) // "Saving"
slice(title, 7) // "Private Ryan"
slice(title, -4) // "Ryan"
slice(title, 0, -4) // "Saving Private"
Slice is special
slice() is the only string function that cannot be used with the dot index (i.e. you cannot call title.slice(0, 6)
).
You could either use slice(title, 3, 6)
or title[3:6]
.
split(separator)¶
Takes a separator (string
)
Returns a list object of substrings (or null
on error)
Example Usage
title.split(" ") // ["Saving", "Private", "Ryan"]
title.split("a") // ["S", "ving Priv", "te Ry", "n"]
title.split("blah") // ["Saving Private Ryan"]
The reverse of list.join
replace(old, new)¶
Takes two string
values
Returns the string with any instances of substring old replaced with new
Example Usage
title.replace("Ryan", "Gosling") // "Saving Private Gosling"
"aaabbbccc".replace("b", "x") // "aaaxxxccc"
title.replace(slice(title, title.indexOf("P")), "Mr. Banks") // "Saving Mr. Banks"
trim¶
Returns the string stripped of any spacing at the beginning or end
Example Usage
"No change".trim // "No change"
" Strip off some spacing ".trim // "Strip off some spacing"
indexes¶
Returns a list object containing indexes of all characters in the string
Example Usage
title.indexes // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18]
code¶
Returns the unicode code point (int
) of the first character of the string
Example Usage
title.code // 83 (unicode decimal for "S")
title[7:].code // 80 (unicode decimal for "P")
"x".code // 120
The reverse of char
len¶
Returns length of string (int
)
Example Usage
title.len / 19
"abc".len // 3
"".len // 0
lower¶
Returns str in lowercase (string
)
Example Usage
title.lower // "saving private ryan"
upper¶
Returns str in uppercase (string
)
Example Usage
title.upper // "SAVING PRIVATE RYAN"
val¶
Converts str to a number
Returns number
if string represents a valid number, 0
if not
Example Usage
title.val // 0 (no numbers in the title string)
"four".val // 0 (no numbers in this string)
"4".val // 4
"0.816" // 0.816
n = "4 2"
n.val // 0
n[0].val // 4
n.remove(" ").val // 42
The reverse of str
values¶
Returns a list object consisting of all the individual characters in str
Example Usage
title.values // ["S", "a", "v", "i", "n", "g", " ", "P", "r", "i", "v", "a", "t", "e", " ", "R", "y", "a", "n"]
to_int¶
Converts str to an integer
Returns int
if string represents a valid integer, or otherwise returns string
str
Example Usage
"five".to_int // "five" (string)
"0.92".to_int // "0.92" (string)
"5".to_int // 5 (int number)
n = "3 8"
n.to_int // "3 8" (string)
n[0].to_int // 3 (int number)
n.remove(" ").to_int // 38 (int number)
Info
This function is similar to string.val above, but is designed to convert a string to an integer (a whole number), while the val function supports floats (decimal numbers) as well as integers.
Numbers¶
Number Operators¶
Info
For the following operator examples, the variables are defined as below:
a = 8
b = 5
Operator | Operation | Returns | Outcome |
---|---|---|---|
a + b | addition | numeric sum of a and b | 13 |
a - b | subtraction | numeric difference of a and b | 3 |
a * b | multiplication | a multiplied by b | 40 |
a / b | division | a divided by b | 1.6 |
a % b | modulo | remainder after dividing a by b | 3 |
a ^ b | power | a raised to the power of b | 32768 |
a and b | logical and | a * b, clamped to the range [0,1] | 1 |
a or b | logical or | a + b - a*b, clamped to the range [0,1] | 1 |
not a | negation | 1 - abs(a), clamped to the range [0,1] | 0 |
a == b | equality | 1 if a equals b, else 0 | 0 |
a != b | inequality | 1 if a is not equal to b, else 0 | 1 |
a > b | greater than | 1 if a is greater than b, else 0 | 1 |
a >= b | greater than or equal | 1 if a is greater than or equal to b, else 0 | 1 |
a < b | less than | 1 if a is less than b, else 0 | 0 |
a <= b | less than | or equal 1 if a is less than or equal to b, else 0 | 0 |
Number Functions¶
Reference
For the following functions, example snippets will use the variables defined below:
p = 4
q = 0.5
r = 3.142
s = -1
abs(num)¶
Returns the absolute value of num (number
, i.e. int
or float
)
Example Usage
abs(p) // 4
abs(r) // 3.142
acos(num)¶
Returns the arccosine of num in radians (number
)
Example Usage
acos(p) // error: NaN
acos(q) // 1.0471975511966
acos(r) // error: NaN
acos(s) // 3.14159265358979
asin(num)¶
Returns the arcsine of num in radians (number
)
Example Usage
asin(p) // error: NaN
asin(q) // 0.523598775598299
asin(r) // error: NaN
asin(s) // -1.5707963267949
atan(num)¶
Returns the arctangent of num in radians (number
)
Example Usage
atan(p) // 1.32581766366803
atan(q) // 0.463647609000806
atan(r) // 1.26266472700191
atan(s) // -0.785398163397448
tan(rad)¶
Returns the tangent of rad (number
radians)
Example Usage
tan(p) // 1.15782128234958
tan(q) // 0.54630248984379
tan(r) // 0.000407346432737146
tan(s) // -1.5574077246549
cos(rad)¶
Returns the cosine of rad (number
radians)
Example Usage
cos(p) // -0.653643620863612
cos(q) // 0.877582561890373
cos(r) // -0.999999917034452
cos(s) // 0.54030230586814
sin(rad)¶
Returns the sine of rad (number
radians)
Example Usage
sin(p) // -0.756802495307928
sin(q) // 0.479425538604203
sin(r) // -0.000407346398941426
sin(s) // -0.841470984807897
char(int)¶
Returns the unicode character (string
) with code point int
Example Usage
char(120) // "x"
char(98) // "b"
char(82) // "R"
char(9999999) // null
The reverse of string.code
floor(x)¶
Returns a number
floored to the base integer
Example Usage
floor(q) // 0
floor(r) // 3
floor(4.97381) // 4
range(start, end=0, inc=null)¶
Returns a list object containing values from start through to end, in increments of inc
Takes a starting number
, optional end (number
) and optional inc (number
). If inc is not defined, the increment defaults to 1 (if end > start) or otherwise to -1
Example Usage
range(q, p, q) // [0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4]
range(s, p) // [-1, 0, 1, 2, 3, 4] (inc defaults to -1)
range(1, 5) // [1, 2, 3, 4, 5] (inc defaults to 1)
range(2, 10, 2) // [2, 4, 6, 8, 10]
range(1, 10, 2) // [1, 3, 5, 7, 9]
//the only required parameter is the starting number
range(p) // [4, 3, 2, 1, 0] (counts down to default end 0 in increment of -1)
round(x, d=0)¶
Returns a number
rounded to d decimal places
Example Usage
round(q) // 0 (0.5 or less will be rounded down)
round(r) // 3
round(0.51) // 1
round(q, 1) // 0.5
round(r, 1) // 3.1
round(6.275, 2) // 6.27
rnd(seed=null)¶
Returns a random float
between 0 and 1
If seed number
is provided, this seeds the random number generator with the given value
Example Usage
rnd // 0.8272635177252416
rnd // 0.1047530992811462 (random each time)
rnd(9.2) // 819
rnd(9.2) // 819 (same seed means the same result)
Random whole numbers between x and y
If we wanted to generate a random number from 1 to 10:
floor((rnd * 10) + 1)
rnd
to generate a random 0-1 float (e.g. 0.46
), multiplies this by the max (e.g. 0.46 * 10
= 4.6
), adds 1 (5.6
) and then floors the number down to the base integer (5
).
Still, we could even avoid the use of the rnd
function altogether:
r = range(1, 1000) // generates a list of numbers: [1, 2, ... 999, 1000]
r.shuffle // shuffles the list
r.pull // returns the first number from the shuffled list and removes it from the list
r.pull // returns the first number from the updated list
r.pull // we can keep pulling numbers from the shuffled array; no need to instantiate new vars
sign(num)¶
Returns the sign of num, either -1
(if num < 0), 0
(if num == 0), or 1
if num > 0
Example Usage
sign(p) // 1
sign(q) // 1
sign(r) // 1
sign(s) // -1
sign(0) // 0
sqrt(num)¶
Returns the square root of num (number
)
Example Usage
sqrt(p) // 2
sqrt(q) // 0.707106781186548
sqrt(r) // 1.77256875748164
sqrt(s) // NaN (s is a negative int)
sqrt(0) // 0
sqrt(36) // 6
str(n)¶
Returns the string
value of n
Example Usage
str(p) // "4"
str(q) // "0.5"
str(r) // "3.142"
str(s) // "-1"
str("4") // "4" (if the input data is already a string, it will be returned)
str("abc") // "abc"
The reverse of string.val
ceil(x)¶
Returns a number
raised to the next or equal integer
Example Usage
ceil(p) // 4
ceil(q) // 1
ceil(r) // 3
ceil(4.0001) // 5
pi¶
Returns 3.14159265358979
(float
)
Will this ever be useful?
Probably not
Lists¶
Lists are essentially arrays, but they may consist of mixed data types, including strings, numbers, maps and other lists.
All these are valid lists
nums = [2, 4, 6, 8]
fruits = ["apple", "orange", "banana", "pineapple"]
mixed = [5, "six", [7, 8, 9], { 10: "ten" }, fruits]
Unlike maps, lists are ordered, and like normal arrays they are accessible by an index (starting with zero).
Example Code
list = ["a", "b", "c", "d", 1, 2, 3]
print(list[0]) // "a"
print(list[3]) // "d"
print(list[5]) // 2
print(list[-4]) // "d"
List Operators¶
Like strings, lists can be sliced, replicated, and concatenated.
You can concatenate two lists with +
, replicate or cut a list with *
and /
, and access elements or sublists using the slice function or operator.
Info
For the following operator examples, the variables are defined as below:
u = [1, 2, 3, 4, 5] // in practice, we could simply use 'range(1, 5)'
v = ["almond", "banana", "cucumber", "date", "edamame"]
w = range(6, 8)
x = ["G", "H"]
n = 2
Operator | Operation | Returns | Outcome |
---|---|---|---|
u + w | concatenation | list formed by concatenating w to u |
[1,2,3,4,5,6,7,8] |
x * n | replication | x repeated n times |
["G", "H", "G", "H"] |
x * 1.5 | fractions of x for float 1.5 |
["G", "H", "G"] |
|
x / n | division | equivalent to x * (1/n) |
["G"] |
v[n] | index | element n of v (indexes start at 0; negative indexes count from the end) |
"cucumber" |
u[:n] | left slice | sublist of u up to but not including element n |
[1, 2] |
u[n:] | right slice | sublist of u from element n to the end |
[3, 4, 5] |
v[n:5] | slice | sublist of v from index n up to but not including element 5 |
["cucumber", "date", "edamame"] |
Remember: lists are mutable
Unlike strings, when you slice, divide, replicate or concatenate lists, they are modified and any mutations are reflected in references to that list.
Be careful with shared references
a = [1, 2, 3] // creates a list assigned to a
b = a // assigns the same list to variable b
a[-1] = 5 // changes the last element of the list to '5'
print(b) // prints '[1, 2, 5]'
In the example above, b
is a reference of a
, and both variables
share a reference to the same list.
If we want to duplicate a
as
a new list in its own right, a quick shortcut is to slice the list from
index zero, returning a new list for b
containing all the elements
from a
:
Duplicate a list
a = [1, 2, 3] // creates a list assigned to a
b = a[0:] // duplicates the entire list a to variable b
a[-1] = 5 // changes the last element of the first list to '5'
print(a) // prints '[1, 2, 5]'
print(b) // prints '[1, 2, 3]' (the second list was not modified)
List Functions¶
Reference
For the following functions, example snippets will use the variables defined below:
companies = ["Google", "Twitter", "Facebook", "Apple", "Microsoft"]
numbers = [2, 4, 6, 8]
mixed = [companies, numbers] //[["Google","Twitter","Facebook","Apple","Microsoft"], [1,2,3,4,5]]
mixed
above will be a shared reference of the two vars
numbers
and companies
, so mutations to either of the contents of
these lists will result in a change to mixed
.
slice(list, start, end)¶
Takes a list, start (int
index) and optional end (int
index)
Returns a separate list containing the elements in list
from index start
to index end
.
Example Usage
slice(companies, 1, 3) // ["Twitter", "Facebook"]
print(companies) // ["Google", "Twitter", "Facebook", "Apple", "Microsoft"]
slice(companies, 2) // ["Facebook", "Apple", "Microsoft"]
Slice is special
slice() is the only list function that cannot be used with the dot index (i.e. you cannot call companies.slice(1, 3)
).
You could either use slice(companies, 1, 3)
or companies[1:3]
.
hasIndex(index)¶
Does index (int
) exist in the list?
Returns 1
(true) if the list contains the index or 0
(false) if not
Example Usage
companies.hasIndex(4) // 1
companies.hasIndex(5) // 0
numbers.hasIndex(2) // 1
numbers.hasIndex(4) // 0
if companies.hasIndex(4) then
print(companies[4]) // prints "Microsoft"
end if
indexOf(val, begin=null)¶
Get the index of the first element matching val (any
). Optionally searches after begin (int
index)
Returns index (int
) of the first matching element or null
if there are no matches
Example Usage
companies.indexOf("Google") // 0
companies.indexOf("Apple") // 3
companies.indexOf("Yahoo") // null
numbers.indexOf(2) // 0
numbers.indexOf(6) // 2
multi.indexOf(numbers) // 1
numbers.push("nine")
numbers.push({ "ten": 10 }) // [2, 4, 6, 8, "nine", {"ten": 10}]
numbers.indexOf({"ten": 10}) // 5
companies.push("Twitter") // ["Google", "Twitter", "Facebook", "Apple", "Microsoft", "Twitter"]
companies.indexOf("Twitter") // 1
companies.indexOf("Twitter", 1) // 5
remove(index)¶
Removes an item from the list with the provided index (int
)
Example Usage
companies.remove(1)
print(companies) // ["Google", "Facebook", "Apple", "Microsoft"]
companies.remove(companies.indexOf("Apple"))
print(companies) // ["Google", "Facebook", "Microsoft"]
Mutable list functions
List objects can be modified in their place by list.push, list.pop, list.pull, list.remove, list.shuffle, list.sort, and list.reverse
join(separator)¶
Takes a separator (string
)
Concatenates all items within the list (any non-string items will be converted to a string) and returns them in a single string (separated by the separator)
Example Usage
companies.join(" ") // "Google Twitter Facebook Apple Microsoft"
companies.join("/") // "Google/Twitter/Facebook/Apple/Microsoft"
The reverse of str.split
push(val)¶
Pushes a new value (any
) onto the end of the list
Returns the updated list
(and updates the list in its place)
Example Usage
companies.push("Amazon") // ["Google", "Twitter", "Facebook", "Apple", "Microsoft", "Amazon"]
numbers.push(10) // [2, 4, 6, 8, 10]
numbers.push("twelve") // [2, 4, 6, 8, 10, "twelve"]
numbers.push([14, 16]) // [2, 4, 6, 8, 10, "twelve", [14, 16]]
numbers.push({18: "eighteen"}) // [2, 4, 6, 8, 10, "twelve", [14, 16], {18: "eighteen"}]
print(numbers) // [2, 4, 6, 8]
numbers.push(10).push(12) // [2, 4, 6, 8, 10, 12]
pop¶
Returns the last element of the list, and removes that element from the list
Example Usage
companies.pop // "Microsoft"
print(companies) // ["Google", "Twitter", "Facebook", "Apple"]
multi.pop // [2, 4, 6, 8]
pull¶
Returns the first element of the list, and removes that element from the list
Example Usage
companies.pull // "Google"
print(companies) // ["Twitter", "Facebook", "Apple", "Microsoft"]
multi.pull // ["Twitter", "Facebook", "Apple", "Microsoft"]
//remember the first item of 'multi' references 'companies', from which we just pulled "Google"
shuffle¶
Randomly rearranges the elements in the list
Returns null
Example Usage
companies.shuffle
print(companies) // ["Microsoft", "Twitter", "Google", "Apple", "Facebook"]
reverse¶
Reverses the list, rearranging the elements in reverse order.
Example Usage
companies.reverse
print(companies) // ["Microsoft", "Apple", "Facebook", "Twitter", "Google"]
sort(key=null)¶
Sorts the list alphanumerically. Where the list contains map objects, sorts by value of an optional given key (any
)
Returns the updated list
(and updates the list in its place)
Example Usage
companies.sort
print(companies) // ["Apple", "Facebook", "Google", "Microsoft", "Twitter"]
print([4,2,1,3].sort) // [1, 2, 3, 4]
people = [{"first_name":"John", "last_name":"Albertson", "age":32},]
people.push({"first_name":"Albert", "last_name":"Johnson", "age":41})
people.push({"first_name":"Stephen", "last_name":"Roberts", "age":26})
people.push({"first_name":"Robert", "last_name":"Stephenson", "age":54})
people.push({"first_name":"Billy", "last_name":"Zane", "age":21})
people.sort("first_name")
for p in people
print(p.first_name) // loops through people, printing Albert, Billy, John, Robert, and Stephen
end for
for p in people.sort("last_name")
print(p.first_name) // loops through people, printing John, Albert, Stephen, Robert, and Billy
end for
for p in people.sort("age")
print(p.first_name) // loops through people, printing Billy, Stephen, John, Albert, and Robert
end for
indexes¶
Returns a list object containing the list's indexes
Example Usage
companies.indexes // [0, 1, 2, 3, 4]
numbers.indexes // [0, 1, 2, 3]
len¶
Returns the length of the list (int
)
Example Usage
companies.len // 5
numbers.len // 4
multi.len // 2
sum¶
Returns the total (int
) of all numeric values in the list
This applies to numbers
only and excludes strings, lists and maps within the list
Example Usage
companies.sum // 0 (companies contains no numbers)
numbers.sum // 20
[14, 9.27, 3.352].sum // 26.622
values¶
Returns a list containing all the values within the list
Example Usage
companies.values // ["Google", "Twitter", "Facebook", "Apple", "Microsoft", "Amazon"]
Maps¶
Maps are essentially dictionaries (somewhat like JSON), consisting of key and value pairs.
Map values may be retrieved by dot index (map.key
) or index (map["key"]
)
Example Code
y = {"key": "value"}
print(y.key) //prints 'value'
print(y["key"]) //prints 'value'
print({"colour": "green"}.colour) //prints 'green'
print({"colour": "green"}["colour"]) //prints 'green'
There are exceptions to the dot index:
Example Code
try = {"1234": "I declare a thumb war", "fruits": {"apple": "green", "banana": "yellow"}}
print(try["1234"]) //prints 'I declare a thumb war'
print(try.1234) //error - the dot index must begin with a letter
print(try.fruits.banana) //prints 'yellow'
choice = "apple"
print(try.fruits.choice) // error - no parsing a variable as a dot index
print(try.fruits[choice]) // error - mixing dot and regular indexes
print(try["fruits"][choice]) //prints 'green'
Map keys can be numbers
, but remember this means you must use the regular index (because the dot index only works when the key begins with a letter):
Example Code
z = {29: "something awesome", 12: "something excellent", "37": "something fancy", 0.42: "42%"}
print(z.29) //error: dot index needs a key that starts with a letter
print(z["29"]) //error: key not found
print(z[29]) //prints 'something awesome'
print(z[13]) //error: key not found
print(z[37]) //prints 'something fancy'
print(z[0.42]) //prints '42%'
print(z[0]) //error. Maps are not lists. No numerical indexes unless the index is a defined key.
Quick Challenge
map = {"brands": ["Apple", "Microsoft", "Facebook", "Google"]}
1 2 3 4 |
|
1 2 3 4 |
|
Example of Maps inside Maps
Let's apply the above knowledge and write a short script that prints: Your name is John Doe and your favourite thing is something
(random key and its matching value)
info = {"first_name": "John", "last_name": "Doe", "favourites": {"game": "Grey Hack", "flavour": "mango", "website": "GHTools.xyz", "colour": "red", "movie": "Groundhog Day"}}
r = info.favourites.indexes //returns a list object of favourites map keys
r.shuffle //shuffles the list of favourites map keys
f = r.pull //returns the first key from the list
print("Your name is " + info.first_name + " " + info.last_name + " and your favourite " + f + " is " + info["favourites"][f])
Map Functions¶
Reference
For the following functions, example snippets will use the variables defined below:
map = {0: "zero", "1": "one", "two": 2, "more": [3, 4, 5]}
hasIndex(key)¶
Does map.key exist? The key may be a string or integer.
Returns 1
(true) or 0
(false)
Example Usage
map.hasIndex(0) // 1
map.hasIndex(1) // 0
map.hasIndex("1") // 1
map.hasIndex("two") // 1
map.hasIndex("five") // 0
indexOf(value, begin=null)¶
Get the index (in maps, this will be the key) of a value. Optionally searches after begin (any
key)
Returns key (string
or int
) or null
(key/value not found)
Example Usage
map.indexOf("zero") // 0
map.indexOf("one") // "1"
map.indexOf(2) // "two"
map.indexOf([3,4,5]) // "more"
map.indexOf("five") // null
map.indexOf("zero", 0) // null
map[99] = 2 // {0: "zero", "1": "one", "two": 2, "more": [3, 4, 5], 99: 2}
map.indexOf(2, "two") // 99
First key only
If the map contains more than one key with the same value, this function will return only the first key
push(key)¶
Pushes a new key (string
or number
) onto the end of map, with value of 1
Returns the updated map
object
Example Usage
map.push("j") //the map object is updated
print(map) //print the updated map
// {0: "zero", "1": "one", "two": 2, "more": [3, 4, 5], "j": 1}
print(map.push("k")) // push also returns the map object
// {0: "zero", "1": "one", "two": 2, "more": [3, 4, 5], "j": 1, "k": 1}
map["l"] = 12; //alternative to push, allowing us to declare the value
map[6] = "six";
// {0: "zero", "1": "one", "two": 2, "more": [3, 4, 5], "j": 1, "k": 1, "l": 12, 6: "six"}
Remember: maps are mutable
Map objects can be modified in their place by map.push, map.pop, map.remove, and map.shuffle
remove(key)¶
Remove an item from a map, by its key (string
or int
)
Returns 1
(true) or 0
(false)
Example Usage
map.remove(0)
map.remove("two")
print(map) // {"1": "one", "more": [3, 4, 5]}
print(map.remove("more")) // 1 (success)
print(map.remove("nope")) // 0 (fail, as this key does not exist)
map.remove(map.indexOf([3,4,5])) //remove an item from map by its value
print(map) // {"1": "one"}
indexes¶
Returns a list object containing all the map keys
Example Usage
map.indexes // [0, "1", "two", "more"]
print(map.indexes[2]) //error: using a list in this way requires a reference
list = map.indexes //instantiates the list to this variable
print(list[2]) //prints the third key: "two"
len¶
Returns the length of the map (int
)
Example Usage
map.len // 4
pop¶
Returns the key (string
or int
) of the first element in the map, and removes that element from the map
Example Usage
map.pop // 0
desserts = {"peach": "cobbler", "apple": "pie"}
desserts.pop // "peach"
print(desserts) // {"apple": "pie"}
shuffle¶
Randomly remaps values in a map, leaving the keys in their original order
Returns null
Example Usage
map.shuffle
print(map) // {0: 2, "1": "zero", "two": [3, 4, 5], "more": "one"}
map.shuffle
print(map) // {0: [3, 4, 5], "1": 2, "two": "zero", "more": "one"}
sum¶
Returns the total (int
) of all numeric values in the map
This applies to numbers
only and excludes strings, lists and maps within the map
Example Usage
map.sum // 2 (2 is the only sole integer value in our example map)
values¶
Returns a list containing all the values within the map
Example Usage
map.values // ["zero", "one", 2, [3, 4, 5]]