Array in Scol » History » Version 1
iri, 09/24/2012 10:12 PM
| 1 | 1 | iri | h1. Array in Scol |
|---|---|---|---|
| 2 | |||
| 3 | Arrays are helpful. Like any language, an array is a collection of elements, each identifed by an index. Its size is fixed. The access time of any element is always the same. The data type should be unchanged (like C but not like Python). |
||
| 4 | An array can be one dimensional or multi dimensional. |
||
| 5 | |||
| 6 | Note : in Scol, a string (type S) is *not* an array. |
||
| 7 | |||
| 8 | h2. What is the array type ? |
||
| 9 | |||
| 10 | This type is *tab u0* |
||
| 11 | *u0* can be any type, simple or complex. For example : |
||
| 12 | *tab S* : an array of strings |
||
| 13 | *tab [I F]* : an array of tuples |
||
| 14 | *tab [S r1]* : an array of lists |
||
| 15 | *tab [S I tab S]* : an array of tuples with a sub array |
||
| 16 | |||
| 17 | h2. How create an array ? |
||
| 18 | |||
| 19 | By the Scol function *mktab* : mktab <initial_value> <size> |
||
| 20 | |||
| 21 | h3. Example : |
||
| 22 | |||
| 23 | <pre> |
||
| 24 | typeof array = tab I;; |
||
| 25 | |||
| 26 | fun main ()= |
||
| 27 | _showconsole; |
||
| 28 | set array = mktab 1 10; // size = 10, each element at 1 |
||
| 29 | 0;; |
||
| 30 | </pre> |
||
| 31 | |||
| 32 | h2. How to set an element ? |
||
| 33 | |||
| 34 | By its index : |
||
| 35 | |||
| 36 | <pre> |
||
| 37 | typeof array = tab S;; |
||
| 38 | |||
| 39 | fun main ()= |
||
| 40 | _showconsole; |
||
| 41 | set array = mktab nil 5; |
||
| 42 | set array.0 = "Bob"; |
||
| 43 | set array.1 = "Alice"; |
||
| 44 | set array.2 = "Uma"; |
||
| 45 | set array.3 = "Kevin"; |
||
| 46 | set array.4 = "James"; |
||
| 47 | 0;; |
||
| 48 | </pre> |
||
| 49 | |||
| 50 | or, depending on the context : |
||
| 51 | <pre> |
||
| 52 | |||
| 53 | typeof array = tab S;; |
||
| 54 | |||
| 55 | fun setArray (list, index)= |
||
| 56 | if list == nil then |
||
| 57 | 0 // done |
||
| 58 | else |
||
| 59 | ( |
||
| 60 | set array.index = hd list; |
||
| 61 | setArray tl list index+1 |
||
| 62 | );; |
||
| 63 | |||
| 64 | fun main ()= |
||
| 65 | _showconsole; |
||
| 66 | set array = mktab nil 5; |
||
| 67 | setArray "Bob"::"Alice"::"Uma"::"Kevin"::"Jason"::nil 0; |
||
| 68 | 0;; |
||
| 69 | </pre> |
||
| 70 | |||
| 71 | For a multidimensioanl array : |
||
| 72 | |||
| 73 | <pre> |
||
| 74 | set array[indexA][indexB] = value; |
||
| 75 | </pre> |
||
| 76 | |||
| 77 | h2. How to get an element ? |
||
| 78 | |||
| 79 | By its index : |
||
| 80 | |||
| 81 | <pre> |
||
| 82 | set myVariable = array[index]; |
||
| 83 | </pre> |
||
| 84 | |||
| 85 | h2. How get the size of an array ? |
||
| 86 | |||
| 87 | By the Scol function *tabsize* : tabsize <array> or by a recursive function (see [[List in Scol]]). |
||
| 88 | |||
| 89 | h2. How to create an associative array ? |
||
| 90 | |||
| 91 | By a tuple with two elements. |
||
| 92 | For example : |
||
| 93 | <pre> |
||
| 94 | |||
| 95 | typeof array = tab [S I];; |
||
| 96 | |||
| 97 | fun main ()= |
||
| 98 | _showconsole; |
||
| 99 | set array = tab nil 128; |
||
| 100 | set array.0 = ["Bob" 75]; |
||
| 101 | set array.1 = ["Alice" 25]; |
||
| 102 | ... |
||
| 103 | 0;; |
||
| 104 | </pre> |
||
| 105 | |||
| 106 | - Others |
||
| 107 | |||
| 108 | The non standard library _Syspack_ provides others array functions. |
||
| 109 | |||
| 110 | |||
| 111 | License : "CC-BY-SA-2.0":https://creativecommons.org/licenses/by-sa/2.0/ |
||
| 112 | Tutorial by iri |
||
| 113 | Updated by / |