Arrays
An array is a special form of a variable.
Instead of just 1 value, you can store a lot of values in it. It has a bunch of slots.
Normally you would require a separate name for each slot. But when you use an array, you only need the name of the variable containing that array and the index of the specific slot where a specific value is (or will be) stored.
Define an empty array
Defining an empty array can be done like so:
[] var, arr1
It seems like we just define a normal variable arr1 here. However, note that the [] we push onto the stack first is an empty array!
Note that [] is not a single character but the 2 characters [ and ] concatenated.
Define an array with initial values
You can also define an array with initial values, like so:
[100, 200, 300] var, arr2
What is a variable and what is an array?
In effect an 8th variable is a pointer to a “value”. The variable itself has no specific type, the value it points to has a type.
Using the @ word, you dereference the variable and point now directly to the value which has a specific type.
When the value pointed to happens to be an array, a specific action is required to get the desired array element.
In effect an array is a series of pointers to each array element. An array is a type itself. However, the array elements can be of different types.
So first we have to “find”a specific array element before we can establish the type of that element.
Using the a:@ word in combination with an array index number we can now dereference the array element itself.
Dereferencing an array and it’s elements
This is the tricky part of arrays in 8th.
In languages like C or Java, you can reference array elements like so:
arr1[3]
We would expect that we could retrieve the same in 8th something like so:
arr1 3
However that is NOT the case! In 8th we must dereference the variable first using @ and then dereference the array element using an index and the a:@ word:
arr1 @ 3 a:@
So, effectively:
- you first dereference the variable that holds your array using @
- then you use the number of the element to be retrieved from that array (3 in this example)
- and finally you use the a:@ word to retrieve the corresponding element value
You could compare it with a chest of drawers. When you need something in drawer 3:
- you must first reference (go to) the specific chest (array)
- then you use the number of the drawer (here the number 3)
- and finally you open that drawer and retrieve it’s contents
An example to show what happens:
Explanation of each step:
- .s \ show that stack is empty
- [10, 11, 12, 13] var, a \ define an array named a with 4 elements
- .s \ show that stack is empty
- a @ . cr \ here we dereference the variable and get the value
- [10,11,12,13] \ it appears that that value is a 4 element array
- .s \ show that stack is empty
- a @ 3 a:@ . cr \ now we dereference the array element 3 using the word a:@
- \ it appears that element 3 is the number 13, which means that
- \ 8th uses index 0 (zero) for the first element!
- .s \ shows that the stack keeps the reference to the variable a
- \ please have a look in the manual for the SED of a:@
Because the reference is left on the stack (in step 7) you could go on referencing other elements like so:
0 a:@ . cr \ This would fetch array element 0 (zero) which is the first element: 10.
More info about 8th
There is a central website for 8th here: http://8th-dev.com/

No comments:
Post a Comment