Creating an Array: Difference between revisions
Content added Content deleted
m (→J) |
m (→[[Creating an Array#ALGOL 68]]: fix lang tag) |
||
Line 40: | Line 40: | ||
==[[ALGOL 68]]== |
==[[ALGOL 68]]== |
||
As with all ALGOL 68 declarations the programmer has the choice of using the full declaration syntax, or using [[syntactic sugar]] - c.f. "sugared" variables below. |
As with all ALGOL 68 declarations the programmer has the choice of using the full declaration syntax, or using [[wp:syntactic sugar|syntactic sugar]] - c.f. "sugared" variables below. |
||
Example of array of 10 integer types: |
Example of array of 10 integer types: |
||
⚫ | |||
<lang algol> |
|||
HEAP [1:10]INT sugared hnumbers; # from global memory # |
|||
⚫ | |||
LOC [1:10]INT sugared lnumbers1; # from the stack # |
|||
[10]INT sugared lnumbers2; # from the stack - LOC scope is implied #</lang> |
|||
[10]INT sugared lnumbers2; # from the stack - LOC scope is implied # |
|||
</lang> |
|||
Note that the array can be taken from the heap, or stack. |
Note that the array can be taken from the heap, or stack. |
||
Example of array of 3 string types: |
Example of array of 3 string types: |
||
⚫ | |||
<lang algol> |
|||
[3]STRING vwords := ( "these", "are", "arrays" ); # array is a variable and modifiable (:=) #</lang> |
|||
⚫ | |||
⚫ | |||
⚫ | |||
</lang> |
|||
⚫ | |||
⚫ | |||
⚫ | |||
<lang algol> |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
</lang> |
|||
⚫ | |||
The following creates a 3x2 int matrix |
The following creates a 3x2 int matrix |
||
<lang algol68>REF [][]INT number matrix = LOC [3][2] INT; |
|||
<lang algol> |
|||
[3,2]INT sugared number matrix1; # an matrix of integers # |
|||
[3][2]INT sugared number matrix2; # an array of arrays of integers #</lang> |
|||
⚫ | |||
[3][2]INT sugared number matrix2; # an array of arrays of integers # |
|||
</lang> |
|||
⚫ | |||
difference being each row in the matrix must be enclosed in its own braces. |
difference being each row in the matrix must be enclosed in its own braces. |
||
⚫ | |||
<lang algol> |
|||
⚫ | |||
</lang> |
|||
or |
or |
||
⚫ | |||
<lang algol> |
|||
[2][2] STRING sugared funny matrix := ( ("clowns", "are") , ("not", "funny") );</lang> |
|||
⚫ | |||
</lang> |
|||
Further, the arrays can start at any number: |
Further, the arrays can start at any number: |
||
⚫ | |||
<lang algol> |
|||
⚫ | |||
</lang> |
|||
If the array is expected to change size in future, then the programmer can also declare it FLEX. |
If the array is expected to change size in future, then the programmer can also declare it FLEX. |
||
⚫ | |||
<lang algol> |
|||
⚫ | |||
</lang> |
|||
This next piece of code creates an array of references to integers. The value of |
This next piece of code creates an array of references to integers. The value of |
||
each integer "pointed at" is the square of it's index in the array. |
each integer "pointed at" is the square of it's index in the array. |
||
⚫ | |||
<lang algol> |
|||
⚫ | |||
FOR index FROM LWB array of pointers to ints TO UPB array of pointers to ints DO |
FOR index FROM LWB array of pointers to ints TO UPB array of pointers to ints DO |
||
array of pointers to ints[index] := HEAP INT := i*i # allocate global memory # |
array of pointers to ints[index] := HEAP INT := i*i # allocate global memory # |
||
OD |
OD</lang> |
||
</lang> |
|||
{{works with|ALGOL 68|Standard - no extensions to language used}} |
{{works with|ALGOL 68|Standard - no extensions to language used}} |
||
Line 101: | Line 83: | ||
{{works with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release 1.8.8d.fc9.i386}} |
{{works with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release 1.8.8d.fc9.i386}} |
||
⚫ | |||
<lang algol> |
|||
⚫ | |||
MODE MAT = FLEX[0,0]REAL; |
MODE MAT = FLEX[0,0]REAL; |
||
# MODE STRING = FLEX[0]CHAR; builtin # |
# MODE STRING = FLEX[0]CHAR; builtin # |
||
MODE BOOLS = FLEX[0]BOOL; # BITS is builtin in the standard # |
MODE BOOLS = FLEX[0]BOOL; # BITS is builtin in the standard #</lang> |
||
</lang> |
|||
Initialization by an aggregate using positional and keyed notations: |
Initialization by an aggregate using positional and keyed notations: |
||
<lang |
<lang algol68>VEC x := (1, 4, 5); |
||
VEC x := (1, 4, 5); |
|||
[100]INT y; FOR i TO UPB y DO y[i]:=0 OD; FOR i FROM 5 TO 20 DO y[i]:= 2 OD; y[2]:=y[3]:= 1; |
[100]INT y; FOR i TO UPB y DO y[i]:=0 OD; FOR i FROM 5 TO 20 DO y[i]:= 2 OD; y[2]:=y[3]:= 1; |
||
MAT e := ((1, 0), (0, 1)); |
MAT e := ((1, 0), (0, 1)); |
||
Line 117: | Line 96: | ||
[2]BOOL b := (TRUE, TRUE); |
[2]BOOL b := (TRUE, TRUE); |
||
BITS byte := (TRUE, TRUE); |
BITS byte := (TRUE, TRUE); |
||
SKIP |
SKIP</lang> |
||
</lang> |
|||
==[[AmigaE]]== |
==[[AmigaE]]== |