|
| 1 | +#ARRAYS by DevKev for version 1.9.10# |
| 2 | + |
| 3 | +#Declare a variable as an array using "[" and "]"# |
| 4 | + emptyArray = []; |
| 5 | + println $emptyArray; |
| 6 | + |
| 7 | + |
| 8 | +#To declare filled arrays, use the same method, but separate different entries with a space character inside "[" and "]"# |
| 9 | + filledStringArray = ["test1" "test2" "test3"]; |
| 10 | + filledIntegerArray = [1 2 3]; |
| 11 | + println $filledStringArray; |
| 12 | + |
| 13 | + |
| 14 | +#All arrays are dynamic and can hold different datatypes and can resolve variables and wrapped commands# |
| 15 | + chaoticArray = ["String" (1 + 1) $filledStringArray {}]; #<- A single array with 4 different data types: String, Array, Block# |
| 16 | + println $chaoticArray; |
| 17 | + |
| 18 | + |
| 19 | +#You can also create arrays with multiple dimensions by just putting arrays into arrays |
| 20 | +This will create a grid of 4x4 made of "+" when printed properly# |
| 21 | + grid = [[+ - + +] [+ + + +] [+ + + +] [+ + + +]]; |
| 22 | + |
| 23 | + |
| 24 | +#You can access array indices like any other language.# |
| 25 | + index = 0; |
| 26 | + println $filledStringArray[0]; |
| 27 | + println $filledStringArray[$index]; #<- Using a variable. Maybe the famous $i in for- loops?# |
| 28 | + println $grid[0][1]; |
| 29 | + |
| 30 | + |
| 31 | +#You can modify array indices like in Tutorial 2 without the "$"# |
| 32 | + index = 1; |
| 33 | + filledStringArray[0] = "First Element!"; |
| 34 | + grid[0][$index] = "+"; #<- Lets repair the grid!# |
| 35 | + println $filledStringArray[0]; |
| 36 | + println $grid; |
| 37 | + |
| 38 | + |
| 39 | +#There are 3 methods to extend arrays: |
| 40 | +You can add array elements simply by choosing any index that is out of bounds (>= 0) |
| 41 | +You can also use the "push [value] [array]" to extend arrays |
| 42 | +The third method would be to use the "[???] + [???]" command# |
| 43 | + emptyArray[5] = 10; #This will automatically fill 0 - 4 with $null and make the array length 6# |
| 44 | + println $emptyArray; |
| 45 | + |
| 46 | + #Here is a list of all the methods to expand an array:# |
| 47 | + #Method 1:# |
| 48 | + push "10" $filledIntegerArray; #Expand by using the "push" command (push the value "10" into $filledIntegerArray)# |
| 49 | + |
| 50 | + #Method 2:# |
| 51 | + length = (length $filledIntegerArray); #Expand by finding out the length and expanding with the "out-of-bounds" method# |
| 52 | + filledIntegerArray[$length] = 10; |
| 53 | + |
| 54 | + #Method 3:# |
| 55 | + $filledIntegerArray + "newValue" #add the String "newValue" to the array using the "+" command. But this method can be confusing, because you need to use the $ sign even if you modify the variable |
| 56 | + filledIntegerArray = ($filledIntegerArray + "newValue") #This method has the same result and is a bit more readable# |
| 57 | + |
| 58 | +#You can remove array elements using the "pop [array] [index]" command# |
| 59 | + pop $emptyArray 5; |
| 60 | + println $emptyArray; |
| 61 | + |
| 62 | +#You can check Array types (If, for example an Array has only Strings (-> "String Array") |
| 63 | +or the array contains multiple data types) using the "typeof" command |
| 64 | +IMPORTANT: If you want to use the "typeof" command to check array types, remember to put an array indicator (@) at the beginning! |
| 65 | +@String != String (-> Array of Strings does not equal a single String)# |
| 66 | + println ($filledStringArray typeof "@String"); #prints true: There are only strings in the array# |
| 67 | + println ($chaoticArray typeof "@?"); #prints true: This array contains multiple data types, is "any" (?)# |
| 68 | + println ($chaoticArray typeof "@block"); #println true: WHY?! This is kind of a special case. Since the type is "any", it is also "block", "string", "array" and so on and will always return true no matter what array type# |
| 69 | + println ($filledStringArray typeof "@?"); #prints false: This is the String array# |
| 70 | + |
0 commit comments