This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
harlowe:array [2017/06/19 01:05] l |
harlowe:array [2019/04/16 03:52] (current) l |
||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====Array data==== | ||
There are occasions when you may need to work with a whole sequence of values at once. | There are occasions when you may need to work with a whole sequence of values at once. | ||
For example, a sequence of adjectives (describing the player) that should be printed depending | For example, a sequence of adjectives (describing the player) that should be printed depending | ||
Line 16: | Line 17: | ||
to remove an item from, you can use an expression, in brackers, after it: ''%%$array's ($pos - 3)%%''. | to remove an item from, you can use an expression, in brackers, after it: ''%%$array's ($pos - 3)%%''. | ||
- | To see if arrays contain certain values, you can use the ''%%contains%%'' and ''%%is in%%'' operators like so: ''%%$array contains 1%%'' | + | To see if arrays contain certain values, you can use the ''%%contains%%'' and ''%%is in%%'' operators like so: ''%%$array contains 1%%'' is true if it contains the number 1 anywhere, and false if it does not. ''%%1 is in $array%%'' is another way to write that. |
- | is true if it contains the number 1 anywhere, and false if it does not. ''%%1 is in $array%%'' is another way to write that. | + | If you want to check if an array contains some, or all of the values, in another array (without needing to be in the same order), you can compare with a special ''%%any%%'' or ''%%all%%'' name on the other array: ''%%$array contains any of (a:2,4,6)%%'', and ''%%$array contains all of (a:2,4,6)%%'' will check if ''%%$array%%'' contains some, or all, of the numbers 2, 4 and 6. |
- | If you want to check if an array contains some, or all of the values, in another array, you can compare with a special | + | |
- | ''%%any%%'' or ''%%all%%'' name on the other array: ''%%$array contains any of (a:2,4,6)%%'', and ''%%$array contains all of (a:2,4,6)%%'' | + | |
- | will check if ''%%$array%%'' contains some, or all, of the numbers 2, 4 and 6. | + | |
(Incidentally, ''%%any%%'' and ''%%all%%'' can also be used with other operators, like ''%%is%%'', ''%%is not%%'', ''%%>%%'', ''%%<%%'', ''%%>=%%'', and ''%%<=%%'', | (Incidentally, ''%%any%%'' and ''%%all%%'' can also be used with other operators, like ''%%is%%'', ''%%is not%%'', ''%%>%%'', ''%%<%%'', ''%%>=%%'', and ''%%<=%%'', | ||
to compare every value in the array with a number or other value. For instance, ''%%all of (a:2,4) >= 2%%'' is true, as is | to compare every value in the array with a number or other value. For instance, ''%%all of (a:2,4) >= 2%%'' is true, as is | ||
''%%any of (a:2,4) >= 4%%''.) | ''%%any of (a:2,4) >= 4%%''.) | ||
+ | |||
+ | For a more thorough check of the contents of an array, you can use ''%%matches%%'' and a [[harlowe:datatype|datatype]] pattern. For instance, ''%%$array matches (a: num, num)%%'' lets you check that $array contains exactly two numbers, and ''%%$array matches (a: 2, num)%%'' lets you check that $array contains only 2 followed by another number. See the datatype article for more details. | ||
Arrays may be joined by adding them together: ''%%(a: 1, 2) + (a: 3, 4)%%'' is the same as ''%%(a: 1, 2, 3, 4)%%''. | Arrays may be joined by adding them together: ''%%(a: 1, 2) + (a: 3, 4)%%'' is the same as ''%%(a: 1, 2, 3, 4)%%''. | ||
Line 46: | Line 46: | ||
^ Operator ^ Purpose ^ Example ^ | ^ Operator ^ Purpose ^ Example ^ | ||
- | | `is` | Evaluates to boolean `true` if both sides contain equal items in an equal order, otherwise `false`. | `(a:1,2) is (a:1,2)` (is true) | + | |''%%is%%'' | Evaluates to boolean''%%true%%'' if both sides contain equal items in an equal order, otherwise''%%false%%''. |''%%(a:1,2) is (a:1,2)%%'' (is true)| |
- | | `is not` | Evaluates to `true` if both sides differ in items or ordering. | `(a:4,5) is not (a:5,4)` (is true) | + | |''%%is not%%'' | Evaluates to''%%true%%'' if both sides differ in items or ordering. |''%%(a:4,5) is not (a:5,4)%%'' (is true)| |
- | | `contains` | Evaluates to `true` if the left side contains the right side. | `(a:"Ape") contains "Ape"`, `(a:(a:99)) contains (a:99)`, `(a:1,2) contains any of (a:2,3)`, `(a:1,2) contains all of (a:2,1)` | + | |''%%contains%%'' | Evaluates to''%%true%%'' if the left side contains the right side. |''%%(a:"Ape") contains "Ape"%%'',''%%(a:(a:99)) contains (a:99)%%'',''%%(a:1,2) contains any of (a:2,3)%%'',''%%(a:1,2) contains all of (a:2,1)%%''| |
- | | `is in` | Evaluates to `true` if the right side contains the left side. | `"Ape" is in (a:"Ape")`, `(a:99) is in (a:(a:99))`, `any of (a:2,3) is in (a:1,2)`, `all of (a:2,1) is in (a:1,2)` | + | |''%%is in%%'' | Evaluates to''%%true%%'' if the right side contains the left side. |''%%"Ape" is in (a:"Ape")%%'',''%%(a:99) is in (a:(a:99))%%'',''%%any of (a:2,3) is in (a:1,2)%%'',''%%all of (a:2,1) is in (a:1,2)%%''| |
- | | `+` | Joins arrays. | `(a:1,2) + (a:1,2)` (is `(a:1,2,1,2)`) | + | |''%%+%%'' | Joins arrays. |''%%(a:1,2) + (a:1,2)%%'' (is''%%(a:1,2,1,2)%%'')| |
- | | `-` | Subtracts arrays, producing an array containing every value in the left side but not the right. | `(a:1,1,2,3,4,5) - (a:1,2)` (is `(a:3,4,5)`) | + | |''%%-%%'' | Subtracts arrays, producing an array containing every value in the left side but not the right. |''%%(a:1,1,2,3,4,5) - (a:1,2)%%'' (is''%%(a:3,4,5)%%'')| |
- | | `...` | When used in a macro call, it separates each value in the right side. | `(a: 0, ...(a:1,2,3,4), 5)` (is `(a:0,1,2,3,4,5)`) | + | |''%%...%%'' | When used in a macro call, it separates each value in the right side. |''%%(a: 0, ...(a:1,2,3,4), 5)%%'' (is''%%(a:0,1,2,3,4,5)%%'')| |
- | | `'s` | Obtains the item at the right numeric position, or the `length`, `any` or `all` values. | `(a:"Y","Z")'s 1st` (is "Y"), `(a:4,5)'s (2)` (is 5), `(a:5,5,5)'s length` (is 3) | + | |''%%'s%%'' | Obtains the item at the right numeric position, or the''%%length%%'',''%%any%%'' or''%%all%%'' values. |''%%(a:"Y","Z")'s 1st%%'' (is "Y"),''%%(a:4,5)'s (2)%%'' (is 5),''%%(a:5,5,5)'s length%%'' (is 3)| |
- | | `of` | Obtains the item at the left numeric position, or the `length`, `any` or `all` values. | `1st of (a:"Y","O")` (is "Y"), `(2) of (a:"P","S")` (is "S"), `length of (a:5,5,5)` (is 3) | + | |''%%of%%'' | Obtains the item at the left numeric position, or the''%%length%%'',''%%any%%'' or''%%all%%'' values. |''%%1st of (a:"Y","O")%%'' (is "Y"),''%%(2) of (a:"P","S")%%'' (is "S"),''%%length of (a:5,5,5)%%'' (is 3)| |
+ | | ''%%matches%%'' | Evaluates to boolean true if the array on one side matches the pattern on the other. | ''%%(a:2,3) matches (a: num, num)%%'', ''%%(a: array) matches (a:[[harlowe:a|(a:)]])%%''| | ||
+ | | ''%%is a%%'', ''%%is an%%'' | Evaluates to boolean true` if the right side is array and the left side is an array. | ''%%(a:2,3) is an array%%''| |