Aligning numbers. 4tH 4tH Forth compiler
Reklamë
Reklamë
Chapter 8
4tH arrays
8.1
Aligning numbers
You may find that printing numbers in columns (I prefer "right-aligned") can be pretty hard. That is because the standard word to print numbers (’.’) prints the number and then a trailing space. That is why ’.R’ was added.
The word ’.R’ works just like ’.’ but instead of just printing the number with a trailing space ’.R’ will print the number right-aligned in a field of N characters wide. Try this and you will see the difference:
140 . cr
150 5 .r cr
In this example the field is five characters wide, so ’150’ will be printed with two leading spaces.
8.2
Creating arrays of constants
Making an array of constants is quite easy. First you have to define the name of the array by using the word ’TABLE’ or ’CREATE’ (which is ANS-Forth). Then you specify all its elements. Note that every element is a literal expression. All elements (even the last) are terminated by the word ’,’. An example: create sizes 18 , 21 , 24 , 27 , 30 , 255 ,
Please note that ’,’ is a word! It has to be separated by spaces on both ends.
8.3
Using arrays of constants
Accessing an array of constants is very much like accessing an array of numbers. In an array of numbers you access the 0th element like this: sixteen 0 th @
78
Shkarko
Reklamë