Creating an Array: Difference between revisions
Content added Content deleted
(→{{header|J}}: word fiddling) |
|||
Line 32: | Line 32: | ||
This_Week : Daily_Activities := (Mon..Fri => Work, Others => Fish); |
This_Week : Daily_Activities := (Mon..Fri => Work, Others => Fish); |
||
=={{header|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. |
|||
Example of array of 10 integer types: |
|||
REF []INT numbers = HEAP [1:10] INT; |
|||
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 # |
|||
Note that the array can be taken from the heap, or stack. |
|||
Example of array of 3 string types: |
|||
[]STRING cwords = ( "these", "are", "arrays" ); # array is a constant and read-only (=) # |
|||
[3]STRING vwords := ( "these", "are", "arrays" ); # array is a variable and modifiable (:=) # |
|||
You can also declare the size of the array and initialize the values at the same time: |
|||
REF []INT more numbers = LOC [3]INT := ( 21, 14 ,63 ); |
|||
[]INT sugared more numbers = ( 21, 14 ,63 ); |
|||
For Multi-Dimensional arrays you declare them the same except for a comma in the type declaration. |
|||
The following creates a 3x2 int matrix |
|||
REF [][]INT number matrix = LOC [3][2] INT; |
|||
[3,2]INT sugared number matrix1; # an matrix of integers # |
|||
[3][2]INT sugared number matrix2; # an array of arrays # |
|||
As with the previous examples you can also initialize the values of the array, the only |
|||
difference being each row in the matrix must be enclosed in its own braces. |
|||
[][]STRING string matrix = ( ("I","swam"), ("in","the"), ("freezing","water") ); |
|||
or |
|||
REF [][] STRING funny matrix = LOC [2][2]STRING := ( ("clowns", "are") , ("not", "funny") ); |
|||
[2][2] STRING sugared funny matrix := ( ("clowns", "are") , ("not", "funny") ); |
|||
Further, the arrays can start at any number: |
|||
[-10:10] INT balanced; # for example -10 # |
|||
If the array is expected to change size in future, then the programmer can also declare it FLEX. |
|||
FLEX [-10:10] INT flex balanced; |
|||
This next piece of code creates an array of integers. The value of |
|||
each integer "pointed at" is the square of its index in the array. |
|||
[-10:10] REF INT array of pointers to ints; |
|||
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 # |
|||
OD |
|||
=={{header|AppleScript}}== |
=={{header|AppleScript}}== |
||
AppleScript arrays are called lists: |
AppleScript arrays are called lists: |
||
Line 39: | Line 77: | ||
Lists can contain any objects including other lists: |
Lists can contain any objects including other lists: |
||
set any to {1, "foo", 2.57, missing value, ints} |
set any to {1, "foo", 2.57, missing value, ints} |
||
'''Bold text''' |
|||
=={{header|BASIC}}== |
=={{header|BASIC}}== |