-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharrays_and_maps.plang
109 lines (87 loc) · 2.52 KB
/
arrays_and_maps.plang
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# This Plang script demonstrates some useful constructions of arrays
# and maps.
# Basic Array
var array = ["red", "green", 1, 2]
print(typeof(array)) # prints [Integer | String]
print(array[1]) # prints green
print(array) # prints ["red","green",1,2]
# Basic Map
var map1 = {
'name' = "Grok",
'health' = 200,
'iq' = 75
}
print(typeof(map1)) # prints {"name": String, "health": Integer, "iq": Integer}
print(map1['name']) # prints Grok
print(map1) # prints {"name" = "Grok", "health" = 200, "iq" = 75}
# Array of anonymous Arrays
var arrays = [[1,2], [3,4], [5,6]]
print(arrays[2][1]) # prints 6
print(arrays) # prints [[1,2],[3,4],[5,6]]
print(typeof(arrays)) # prints [[Integer]]
# Map of anonymous Arrays:
var map_arrays = {
'colors' = [ "red", "green", "blue" ],
'shapes' = [ "triangle", "square", "circle" ],
'sizes' = [ "small", "average", "big" ],
}
print(map_arrays['colors'][1]) # prints green
# To add another anonymous Array to the Map:
map_arrays['pets'] = [ "cat", "dog", "hamster" ];
print(map_arrays) # this should print the map as expected
# Array of anonymous Maps
var array_maps = [
{
'name' = "Grok the Caveman",
'health' = 200,
'iq' = 75
},
{
'name' = "Merlin the Wizard",
'health' = 100,
'iq' = 200
},
{
'name' = "Bob the Human",
'health' = 75,
'iq' = 100,
}
]
print(array_maps[1]['name']) # prints Merlin the Wizard
# Map of anonymous Maps
var maps = {
'caveman' = {
'health' = 200,
'iq' = 75,
},
'wizard' = {
'health' = 100,
'iq' = 200,
},
'human' = {
'health' = 75,
'iq' = 100,
}
}
print(maps['wizard']['iq']) # prints 200
# alternativelyy:
print(maps.wizard.iq) # also prints 200
# To add another anonymous Map to the Map:
maps['dragon'] = { 'health' = 500, 'iq' = 150 }
print(maps) # should print caveman, wizard, human and dragon as expected
# Map of Functions
fn exit_function print("exit called")
# the braces aren't necessary here, but this is what it would look like
fn move(direction) {
print($"moving {direction}")
}
var functions = {
'exit' = exit_function
'help' = fn { print("help called") }
'north' = fn { move('north') }
'south' = fn { move('south') }
}
functions['exit']() # prints exit called
functions['help']() # prints help called
functions['north']() # prints moving north
functions['south']() # prints moving south