- Overview
- Multiple assignments of arrays
- Syntax
- Array of values
- Examples
- Multiple assignments of associative arrays
- Syntax
- Example
- Multiple assignments of structures and classes
- Syntax
- Remarks
- Example
- Nesting
- Hyphenation
Multiple assignments of arrays, structures and classes
The multiple assignment of arrays consists in assigning a set of simple values (boolean, integer, string, real, numeric or currency) to the cells of an array. This multiple assignment is also available for the structures and the classes. Multiple assignments of arrays Syntax - 1-dimensional array:
<1-dimensional array> = [ <Value 1>, <Value 2>, ...]
- 2-dimensional array:
<2-dimensional array> = [ [ <Value 1 1>, <Value 1 2>, ...], [ <Value 2 1>, <Value 2 2>, ...], ... ]
- 3-dimensional array:
<3-dimensional array> = [ [ [ <Value 1 1 1>, <Value 1 1 2>, ...], [ <Value 1 2 1>, <Value 1 2 2>, ...], ... ], [ [ <Value 2 1 1>, <Value 2 1 2>;, ...], [ <Value 2 2 1>, <Value 2 2 2>, ...], ... ], ... ]
Array of values Each set of values represents a dimension of the array of values. The sets of values can be nested to create an array of values with several dimensions. The numbers of dimensions in the array of values must be consistent: each dimension must have the same number of sub-dimensions. However, a different number of elements can be found for each dimension. Examples: // Array of values not allowed: // The first value of the set has a sub-dimension // The second value of the set has no sub-dimension [[ <Value 1 1>, <Value 1 2> ], <Value 2> ] // Array of values allowed: // All the values of the set have a sub-dimension // Each sub-dimension has a different number of elements [[ <Value 1 1>, [ <Value 2 1>, <Value 2 2>, <Value 2 3> ], [<Value 3 1>, <Value 3 2> ]] The dimensions of the array of values must correspond to the dimensions of the array to assign. If the dimensions of the array to assign are not sufficient to store the elements found in the array of values, the array to assign is automatically resized. For a fixed array, if the dimensions of the array to assign are not sufficient to store the elements found in the array of values, the array of values is truncated. If the dimensions of the array to assign are greater than the dimensions of the array of values, the non-assigned elements keep their previous values. Examples // Array of powers of 2 Power2 is array of 0 int Power2 = [1, 2, 4, 8, 16, 32, 64, 128, 256] // Array of numbers of days per month // Line 1: non-leap year // Line 2: leap year NumberDays is array of 2 by 12 int NumberDays = [[31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31], [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]] Multiple assignments of associative arrays Syntax <Associative array> = [ [ <Key1>, <Value1> ], [ <Key2>, <Value2> ], ... ] The array of values must only contain 2 couples (Key, Value). Each couple is assigned to the associative array by using the equivalence of the syntax: AssociativeArray[Key] = Value. For an associative array without duplicates: - If the key does not exist yet, the value is added.
- If the key already exists, the value is modified.
For an associative array with duplicates, all the values are added. Example // Associative array of acronyms of countries Country is associative array of strings Country = [["FR", "France"], ["US", "United States"], ["DE", "Germany"]] Multiple assignments of structures and classes Syntax - Structure:
<Structure Instance> = [ <Member 1>, <Member 2>, ...]
- Class:
<Class Instance> = [ <Member 1>, <Member 2>, ...]
Remarks - The members are assigned in the order of declaration.
- For the classes, the inheritances are ignored: the members of the class are directly assigned.
- Limitation: this syntax does not operate on the dynamic structures and classes.
Example State is Structure Name is string Number is int Prefecture is string END Â Allier is State = ["Allier", 3, "Moulins"] The syntaxes for multiple assignments can be used on arrays of structures, instances of structures containing arrays, ... State is Structure Name is string Number is int Prefecture is string END Â ArrState is array of 0 State ArrState = [["Ain", 1, "Bourg-en-Bresse"], ["Aisne", 2, "Laon"], ["Allier", 3, "Moulins"]] Â ArrStateName is associative array of State ArrStateName = [["Ain", ["Ain", 1, "Bourg-en-Bresse"]], ["Aisne", ["Aisne", 2, "Laon"]], ["Allier", ["Allier", 3, "Moulins"]]] You have the ability to describe the arrays of values with line breaks without using the hyphenation syntax (... triple dot). However, the hyphenation syntax must be used to go the next line after the first opening square bracket. For example: State is Structure Name is string Number is int Prefecture is string END Â Allier is State = [... "Allier", 3, "Moulins"] A comma can also be found in front of a closing square bracket: NumberDays = [[31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31], [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31], ]
This page is also available for…
|
|
|