forked from vczh/tinymoe
-
Notifications
You must be signed in to change notification settings - Fork 0
Manipulating Arrays
vczh edited this page Jan 28, 2014
·
3 revisions
Tinymoe has built-in functions for array:
new array of <expression> items
item <expression> of array <expression>
length of array <expression>
set item <expression> of array <expression> to <expression>
-
array of <list>
(This is from the standard library)
So if we want to create a array storing (1, 2, 3, 4, 5), there are two way to do that:
-
- Using the
new array of
- Using the
set numbers to new array of 5 items
repeat with i from 1 to 5
set item i of array numbers to i
end
You should be noticed that array index starts from 1.
-
- Using the
array of
- Using the
set numbers to array of (1, 2, 3, 4, 5)
The second way is very convenient to create literal arrays. But if you don't know the values inside the array in compile time, you may want to use the first way.
After we have created an array, we may want to traverse this array and get those values. The two following samples both create an array of (1, ..., 10) and sum them:
-
- Using index
set numbers to new array of 10 items
repeat with i from 1 to length of array numbers
set item i of array numbers to i
end
set sum to 0
repeat with i from 1 to length of array numbers
add item i of array numbers to sum
end
sum
should be 55 now.
-
- Using standard library functions
set the numbers to array of (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
set sum to 0
repeat with i in numbers
add i to sum
end
sum
should be 55 now.
repeat with i in numbers
traverse all values inside the array for you directly without the information of their indexes. If you want to get indexes alone with values, you may choose the first way.