|
|
|
|
|
- Accessing the associative arrays
- Accessing the elements of the associative arrays
- How to initialize an associative array as soon as it is declared?
- Browsing the associative arrays
- WLanguage functions and arrays
- Associative Array parameters
- Associative array of classes or structures
- Associative array of arrays, queue, stack, list
Associative array (Variable type) In french: Tableau associatif
An associative array is an "advanced" type of array: it can be used to group a series of elements of the same type.. Each element of the array is indexed on any type of information (and not simply on a numeric index, as in the other types of arrays). Remark: This type of array allows quick access to any element, with certain string options.
taTailleFichier is associative array of int
taTailleFichier["Fichier1.txt"] = fSize("Fichier1.txt")
taTailleFichier["Fichier2.txt"] = fSize("Fichier2.txt")
Trace(taTailleFichier["Fichier2.txt"])
sFichier is string
nTaille is int
FOR EACH ELEMENT nTaille,sFichier OF taTailleFichier
Trace(StringBuild("%1 : %2", sFichier, nTaille))
END
Syntax
Declaring an associative array Hide the details
<Array name> is associative array [(<Options> [, <Default value> [, <Key type> [, <Initial size>]]])] of <Type>
<Array name>: Name of the array variable to be declared. <Options>: Combination of constants used to configure the operating mode of the array:
| | WithDuplicates | This constant is used to allow the duplicates. By default, an associative array cannot contain any duplicates. | ccIgnoreAccent | If the type of key is a string (by default), the indexing ignores the accented characters. | ccIgnoreCase | If the type of key is a string (by default), the indexing is not case sensitive. | ccIgnoreSpace | If the type of key is a string (by default), the indexing ignores the space characters. | ccIgnorePunctuationAndSpace | If the type of key is a string (by default), the indexing is not punctuation sensitive and it ignores the space characters. | WithoutDuplicates (default) | By default, an associative array cannot contain any duplicates. You can leave this constant unspecified. |
The "*" value allows you to keep the default value of the option. <Default value>: Value returned when accessing a non-existing element in an associative array without duplicate. This parameter is ignored in an associative array with duplicates. The default value defined for the array's type is used by default (0, "", False).The "*" value is used to keep the default value of the type. <Key type>: Type of the key used to browse the array. By default, the browse operation is performed in a string key. The authorized types are as follows:
| | wlString | Character string | wlUnicodeString | Unicode string | wlDate | Date | wlDateTime | DateTime | wlInt | 4-byte signed integer | wlInt_8 | 8-byte signed integer | wlUnsignedInt_4 | 4-byte unsigned integer | wlUnsignedInt_8 | 8-byte unsigned integer | wlTime | Time | wlCurrency | Currency | wlNumeric | Numeric | wlReal | 8-byte real | The "*" value is used to keep the default key type (string). <Initial size>: Integer. Corresponds to the initial size of the memory area for the associative array.The addition in an associative array may be quite long if the array is resized. In case of multiple additions, we recommend that you oversize the array in order to optimize the additions. The "*" value allows you to keep the default size. <Type>: Type of the elements in the array.
Declaring an associative array parameter Hide the details
<Parameter name> is associative array of <Type>
<Parameter name>: Name of the array variable to be declared. <Type>: Type of the elements in the array.
Remarks Accessing the associative arrays The following properties can be used to handle an associative array: | | Count | Returns the number of occurrences in the array. | Empty | Used to find out whether the array is empty or not. |
The &Array and Array1 = Array2 syntaxes are not allowed. Associative arrays do not allow for reference operators or copying. Accessing the elements of the associative arrays The access to the elements found in an associative array differs depending on whether the associative array accepts duplicates or not. | | Associative array without duplicates | | Exist property | MonTableau[MaClé]..Existe Returns True if the MyKey element exists, False otherwise. | Empty property | Returns True if the MyKey element does not exist, False otherwise. | Count property | MonTableau[MaClé]..Occurrence Returns 1 if the MyKey element exists, 0 if the element does not exist. | Assigning an element | MonTableau[MaClé] = <Valeur> If the MyKey element does not exist, a MyKey element is created and assigned with <Value>. If the MyKey element exists, its value is modified. | | MonTableau[MaClé, Indice] = Valeur A WLanguage error occurs if the index is different from 1. A WLanguage error occurs if the MyKey element does not exist. If the MyKey element exists, its value is modified.
Syntax compatible with the associative arrays with duplicates. | | Creates the entry in the associative array, with the default value specified when declaring the array. | Access to element | If the MyKey element does not exist, returns the default value of the array. If the MyKey element exists, returns the value of the element. | | MonTableau[MaClé, Indice] A WLanguage error occurs if the index is different from 1. A WLanguage error occurs if the MyKey element does not exist. If the MyKey element exists, returns the value of the element.
Syntax compatible with the associative arrays with duplicates. | Address of element | A WLanguage error occurs if the MyKey element does not exist. If the MyKey element exists, returns the address of the element. | | &MonTableau[MaClé, Indice] A WLanguage error occurs if the index is different from 1. A WLanguage error occurs if the MyKey element does not exist. If the MyKey element exists, returns the address of the element.
Syntax compatible with the associative arrays with duplicates. |
| | Associative array with duplicates | | Exist property | MonTableau[MaClé]..Existe Returns True if at least one MyKey element exists, False if the MyKey element does not exist. | Key existence test: Property Empty | Returns True if the MyKey element does not exist, False if there is at least one MyKey element. | Number of elements in a key: Property Count | MonTableau[MaClé]..Occurrence Returns the number of MyKey elements. | Assigning an element | MonTableau[MaClé] = <Valeur> Creates a MyKey element and assigns the specified value to this element (even if MyKey elements already exist). | | MonTableau[MaClé, Indice] = Valeur A WLanguage error occurs if the index is greater than the number of existing elements. A WLanguage error occurs if the MyKey element does not exist. If the MyKey element exists, the value of the element identified by its index is modified. | Access to element | Invalid syntax that triggers a WLanguage error (because several elements can correspond to the same key). | | MonTableau[MaClé, Indice] A WLanguage error occurs if the index is greater than the number of existing elements. A WLanguage error occurs if the MyKey element does not exist. If the MyKey element exists, returns the value of the element identified by its index. | Address of element | Syntax not allowed | | &MonTableau[MaClé, Indice] A WLanguage error occurs if the index is greater than the number of existing elements. A WLanguage error occurs if the MyKey element does not exist. If the MyKey element exists, returns the address of the element identified by its index. |
You have the ability to use the multiple assignment for the associative arrays. How to initialize an associative array as soon as it is declared? To initialize an associative array as soon as it is declared, all you have to do is add the "key/value" couples by using
the [ ] operator (square brackets). The syntax is as follows: taMonTableau est un tableau associatif de xxx = ... [ [<clé1>, <valeur1>], [<clé2>, <valeur2>], ... ] For example:
taTauxTVA is associative array of reals = ...
[["Normale", 0.2], ["Intermédiaire", 0.1], ["Réduite", 0.055]]
Browsing the associative arrays WLanguage functions and arrays Several WLanguage functions can be used to handle the associative arrays. For more details, see Array functions. This allows you to delete an element from an associative array by using Delete (or ArrayDelete) via the following syntax: Supprime(NomTableau, clé) TableauSupprime(NomTableau, Clé) Associative Array parameters - The type of elements found in the associative array passed as parameter must be the same as the declaration type.
- An associative array cannot be passed by value. A variable must necessarily be passed as parameter.
Example:
PROCEDURE AjouteClient(taClient is associative array of CClient,
sNom is string,
sInfo is string)
c is CClient(sNom, sInfo)
taClient[sNom] = c
- No checks are performed at compile time: checks are only performed at runtime.
- Associative arrays do not allow for reference operators or copying.
Associative array of classes or structures If you are using an associative array of classes or structures, the different instances are automatically copied during the additions. If you are using an associative array of dynamic classes or dynamic structures, the classes or the structures must e allocated during addition (the freeing operations are automatically performed). Examples:
clObj is cLMaClasse
taAssociatif is associative array of cLMaClasse
clObj:nMembre = 0
taAssociatif["Association"] = clObj
clObj:nMembre = 2
Info(taAssociatif["Association"]:nMembre)
pclObj is cLMaClasse dynamic
taAssociatif is associative array of cLMaClasse dynamic
pclObj = new cLMaClasse
pclObj:nMembre = 0
taAssociatif["Association"] = pclObj
pclObj:nMembre = 2
Info(taAssociatif["Association"]:nMembre)
Accessing the members of a non-existing element in an associative array of structures or classes does not automatically create the instance of the structure or class. The line of code: MonTableauAssociatif["Clé"]:Membre = 5
fails if the "Key" element was not created beforehand.
Related Examples:
|
Unit examples (WINDEV): The associative arrays
[ + ] Using the associative arrays of WLanguage: - Fill an associative array - Access an associative array
|
This page is also available for…
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|