- 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:
<Tableau à 1 dimension> = [ <Valeur 1>, <Valeur 2>, ...]
- 2-dimensional array:
<Tableau à 2 dimensions> = [ [ <Valeur 1 1>, <Valeur 1 2>, ...], [ <Valeur 2 1>, <Valeur 2 2>, ...], ... ]
- 3-dimensional array:
<Tableau à 3 dimensions> = [ [ [ <Valeur 1 1 1>, <Valeur 1 1 2>, ...], [ <Valeur 1 2 1>, <Valeur 1 2 2>, ...], ... ], [ [ <Valeur 2 1 1>, <Valeur 2 1 2>;, ...], [ <Valeur 2 2 1>, <Valeur 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 number of dimensions in the value array 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: // Tableau de valeurs interdit: // La première valeur de la série possède une sous-dimension // La seconde valeur de la série ne possède pas de sous-dimension [[ <Valeur 1 1>, <Valeur 1 2> ], <Valeur 2> ] // Tableau de valeurs autorisé: // Toutes les valeurs de la série possèdent une sous-dimension // Chaque sous-dimension a un nombre différent d'éléments [[ <Valeur 1 1>, [ <Valeur 2 1>, <Valeur 2 2>, <Valeur 2 3> ], [<Valeur 3 1>, <Valeur 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
Puissance2 is array of 0 int
Puissance2 = [1, 2, 4, 8, 16, 32, 64, 128, 256]
NombreJours is array of 2 by 12 int
NombreJours = [[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 <Tableau associatif> = [ [ <Clé1>, <Valeur1> ], [ <Clé2>, <Valeur2> ], ... ] The array of values must only contain 2 couples (Key, Value). Each pair is assigned to the associative array using the equivalent 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
Pays is associative array of strings
Pays = [["FR", "France"], ["US", "Etats-Unis"], ["DE", "Allemagne"]]
Multiple assignments of structures and classes Syntax - Structure:
<Instance structure> = [ <Membre 1>, <Membre 2>, ...]
- Class:
<Instance Classe> = [ <Membre 1>, <Membre 2>, ...]
Remarks - The members are assigned in the order of declaration.
- In the case of classes, inheritance is ignored: class members are directly assigned.
- Limitation: this syntax does not work on dynamic structures and classes.
Example Département is Structure
Nom is string
Numéro is int
Préfecture is string
END
Allier is Département = ["Allier", 3, "Moulins"]
The syntaxes for multiple assignments can be used on arrays of structures, instances of structures containing arrays, ... Département is Structure
Nom is string
Numéro is int
Préfecture is string
END
TabDépartement is array of 0 Département
TabDépartement = [["Ain", 1, "Bourg-en-Bresse"],
["Aisne", 2, "Laon"],
["Allier", 3, "Moulins"]]
TabNomDépartement is associative array of Département
TabNomDépartement = [["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 dots). However, the hyphenation syntax must be used to go the next line after the first opening square bracket. For example: Département is Structure
Nom is string
Numéro is int
Préfecture is string
END
Allier is Département = [...
"Allier", 3, "Moulins"]
A comma can also be found in front of a closing square bracket: NombreJours = [[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…
|
|
|