ONLINE HELP
 WINDEVWEBDEV AND WINDEV MOBILE

This content has been translated automatically.  Click here  to view the French version.
Help / WLanguage / WLanguage syntax / Declaring variables
  • 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
WINDEV
WindowsLinuxJavaReports and QueriesUser code (UMC)
WEBDEV
WindowsLinuxPHPWEBDEV - Browser code
WINDEV Mobile
AndroidAndroid Widget iPhone/iPadIOS WidgetApple WatchMac Catalyst
Others
Stored procedures
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.
Example
// Remplit un tableau associatif avec les tailles de fichiers
taTailleFichier is associative array of int
taTailleFichier["Fichier1.txt"] = fSize("Fichier1.txt")
taTailleFichier["Fichier2.txt"] = fSize("Fichier2.txt")

// Récupère la taille d'un fichier
Trace(taTailleFichier["Fichier2.txt"])

// Parcourt les tailles des fichiers
sFichier is string
nTaille is int

// sFichier permet de récupérer la clé de parcours du tableau associatif
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:
WithDuplicatesThis constant is used to allow the duplicates. By default, an associative array cannot contain any duplicates.
ccIgnoreAccentIf the type of key is a string (by default), the indexing ignores the accented characters.
WEBDEV - Browser code This constant is not available.
ccIgnoreCaseIf the type of key is a string (by default), the indexing is not case sensitive.
ccIgnoreSpaceIf the type of key is a string (by default), the indexing ignores the space characters.
ccIgnorePunctuationAndSpaceIf the type of key is a string (by default), the indexing is not punctuation sensitive and it ignores the space characters.
WEBDEV - Browser code This constant is not available.
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:
wlStringCharacter string
wlUnicodeStringUnicode string
wlDateDate
wlDateTimeDateTime
wlInt4-byte signed integer
wlInt_88-byte signed integer
wlUnsignedInt_44-byte unsigned integer
wlUnsignedInt_88-byte unsigned integer
wlTimeTime
wlCurrencyCurrency
wlNumericNumeric
wlReal8-byte real
The "*" value is used to keep the default key type (string).
WEBDEV - Browser code This parameter is not available.
<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.
WEBDEV - Browser code This parameter is not available.
<Type>:
Type of the elements in the array.
WINDEVWEBDEV - Server codeWindowsLinuxiPhone/iPadIOS WidgetApple WatchMac Catalyst Note: The elements making up the array can also be arrays, fixed arrays, associative arrays, queues, stacks or lists..

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.
WINDEVWEBDEV - Server codeWindowsLinuxiPhone/iPadIOS WidgetApple WatchMac Catalyst Note: The elements making up the array can also be arrays, fixed arrays, associative arrays, queues, stacks or lists..
Remarks

Accessing the associative arrays

The following properties can be used to handle an associative array:
CountReturns the number of occurrences in the array.
EmptyUsed 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.
AndroidAndroid Widget Property not available.
Empty property
MonTableau[MaClé]..Vide

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.
MonTableau[MaClé]++

Creates the entry in the associative array, with the default value specified when declaring the array.
Access to element
MonTableau[MaClé]

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
&MonTableau[MaClé]

A WLanguage error occurs if the MyKey element does not exist.
If the MyKey element exists, returns the address of the element.
WEBDEV - Browser code This syntax is not available.
&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.
WEBDEV - Browser code This syntax is not available.
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.
AndroidAndroid Widget Property not available.
Key existence test: Property Empty
MonTableau[MaClé]..Vide

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
MonTableau[MaClé]

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
&MonTableau[MaClé]

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.
WEBDEV - Browser code This syntax is not available.

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:
// Mémorise les taux de TVA par leur dénomination
taTauxTVA is associative array of reals = ...
	[["Normale", 0.2], ["Intermédiaire", 0.1], ["Réduite", 0.055]]

Browsing the associative arrays

The elements of an associative array can be browsed by using the FOR EACH syntax specific to 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:
    // Ajoute un client dans le tableau
    PROCEDURE AjouteClient(taClient is associative array of CClient,
    	sNom is string,
    	sInfo is string)
    // Construit le nouvel objet client
    c is CClient(sNom, sInfo)
    // Ajoute le nouvel objet dans le tableau
    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:
// Cas d'un tableau d'objets NON dynamiques
// Objet
clObj is cLMaClasse
// Tableau associatif d'objets
taAssociatif is associative array of cLMaClasse
// Affectation d'un des membres de l'objet
clObj:nMembre = 0
// Ajout dans le tableau associatif. 
// C'est un tableau d'objets non dynamiques, l'objet est donc copié
// L'affectation peut être "longue" s'il y a beaucoup de membres
taAssociatif["Association"] = clObj
// Changement dans l'objet initial
clObj:nMembre = 2
// L'objet a été copié on retrouve donc 0 dans le tableau 
// (valeur au moment de l'affectation)
Info(taAssociatif["Association"]:nMembre)
// Cas d'un tableau d'objets dynamiques
// Objet dynamique
pclObj is cLMaClasse dynamic
// Tableau associatif d'objets dynamiques
taAssociatif is associative array of cLMaClasse dynamic
// Allocation d'un objet dynamique
pclObj = new cLMaClasse
// Affectation d'un des membres de l'objet
pclObj:nMembre = 0
// Ajout dans le tableau associatif. 
// C'est un tableau d'objets dynamiques. 
// C'est donc un nouveau "pointeur" sur le même objet (affectation rapide)
taAssociatif["Association"] = pclObj
// Changement dans l'objet initial
pclObj:nMembre = 2
// Le tableau contient le même objet donc 2 dans le tableau aussi
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.
WINDEVWEBDEV - Server codeWindowsLinuxiPhone/iPadIOS WidgetApple WatchMac Catalyst

Associative array of arrays, queue, stack, list

The following syntaxes are supported:
<variable> est un tableau associatif de tableaux d'entiers
<variable> est un tableau associatif (avecDoublon) de tableaux d'entiers
<variable> est un tableau associatif (avecDoublon,wlEntier) de tableaux d'entiers
<variable> est un tableau associatif de tableaux de 5 entiers
<variable> est un tableau associatif (avecDoublon) de tableaux de 5 entiers
<variable> est un tableau associatif (avecDoublon,wlEntier) de tableaux de 5 entiers

<variable> est un tableau associatif de tableaux fixes de 5 entiers
<variable> est un tableau associatif (avecDoublon) de tableaux fixes de 5 entiers
<variable> est un tableau associatif (avecDoublon,wlEntier) de tableaux fixes de 5 entiers
<variable> est un tableau associatif de tableaux associatifs d'entiers
<variable> est un tableau associatif (avecDoublon) de tableaux associatifs d'entiers
<variable> est un tableau associatif (avecDoublon,wlEntier) de tableaux associatifs d'entiers

<variable> est un tableau associatif de tableaux associatifs (avecDoublon) d'entiers
<variable> est un tableau associatif (avecDoublon) de tableaux associatifs ...
(avecDoublon) d'entiers
<variable> est un tableau associatif (avecDoublon,wlEntier) de tableaux associatifs ...
(avecDoublon) d'entiers
<variable> est un tableau associatif de tableaux associatifs ...
(avecDoublon,wlEntier) d'entiers
<variable> est un tableau associatif (avecDoublon) de tableaux associatifs ...
(avecDoublon,wlEntier) d'entiers
<variable> est un tableau associatif (avecDoublon,wlEntier) de tableaux associatifs ...
(avecDoublon,wlEntier) d'entiers

<variable> est un tableau associatif de files d'entiers
<variable> est un tableau associatif (avecDoublon) de files d'entiers
<variable> est un tableau associatif (avecDoublon,wlEntier) de files d'entiers

<variable> est un tableau associatif de piles d'entiers
<variable> est un tableau associatif (avecDoublon) de piles d'entiers
<variable> est un tableau associatif (avecDoublon,wlEntier) de piles d'entiers

<variable> est un tableau associatif de listes d'entiers
<variable> est un tableau associatif (avecDoublon) de listes d'entiers
<variable> est un tableau associatif (avecDoublon,wlEntier) de listes d'entiers
Example: Associative array of arrays of strings:
// Déclaration
tabClasses is associative array of array <growth> of strings

// Insère un élément dans le tableau associatif 
Insert(tabClasses, "CM2")

// Remplissage des élèves CM2
tabClasses["CM2"][1] = "ALARD Serge"
tabClasses["CM2"][2] = "BERTAU Kelian"

// Insère un élément dans le tableau associatif 
Insert(tabClasses, "CM1")

// Remplissage des élèves CM1
tabClasses["CM1"][1] = "ALONSO Benoit"
tabClasses["CM1"][2] = "CRISTO Benjamin"

FOR EACH UneClasse, sNom of tabClasses
	FOR EACH Elève OF UneClasse
		Trace(sNom + ":" + Elève)
	END
END
Related Examples:
The associative arrays Unit examples (WINDEV): The associative arrays
[ + ] Using the associative arrays of WLanguage:
- Fill an associative array
- Access an associative array
Minimum version required
  • Version 11
This page is also available for…
Comments
Also called a dictionary
An associative Array is also know as a dictionary, for example in Python.
Sebastian
02 Jun. 2015

Last update: 09/24/2024

Send a report | Local help