|
|
|
|
|
- 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 is used to group a set 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 you to quickly access any element, with some options on the strings.
aaFileSize is associative array of int
aaFileSize["File1.txt"] = fSize("File1.txt")
aaFileSize["File2.txt"] = fSize("File2.txt")
Trace(aaFileSize["File2.txt"])
sFile is string
nSize is int
FOR EACH ELEMENT nSize,sFile OF aaFileSize
Trace(StringBuild("%1: %2", sFile, nSize))
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 declare. <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 | Signed 4-byte integer | wlInt_8 | Signed 8-byte integer | wlUnsignedInt_4 | Unsigned 4-byte integer | wlUnsignedInt_8 | Unsigned 8-byte 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 declare. <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 | Returns True if the MyKey element exists, False otherwise. | Empty property | Returns True if the MyKey element does not exist, False otherwise. | Count property | MyArray[MyKey]..Occurence Returns 1 if the MyKey element exists, 0 if the element does not exist. | Assigning an element | 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. | | MyArray[MyKey, Index] = Value 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. | | 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. | | 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 | Returns True if at least one MyKey element exists, False if the MyKey element does not exist. | Checking the existence of a key: Empty property | Returns True if the MyKey element does not exist, False if there is at least one MyKey element. | Number of elements found in a key: Count property | MyArray[MyKey]..Occurence Returns the number of MyKey elements. | Assigning an element | Creates a MyKey element and assigns the specified value to this element (even if MyKey elements already exist). | | MyArray[MyKey, Index] = Value 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). | | 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 | | 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: aaMyArray is associative array of xxx = ... [ [<key1>, <value1>], [<key2>, <value2>], ... ] For example:
aaVATRate is associative array of real = ...
[["Normal", 0.2], ["Intermediate", 0.1], ["Low", 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: Delete(ArrayName, key) ArrayDelete(ArrayName, Key) 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 AddCustomer(aaCustomer is associative array of CCustomer,
sName is string,
sInfo is string)
c is CCustomer(sName, sInfo)
aaCustomer[sName] = c
- No check is performed during the compilation: checks are 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 cLMyClass
aaAssociative is associative array of cLMyClass
clObj:nMember = 0
aaAssociative["Association"] = clObj
clObj:nMember = 2
Info(aaAssociative["Association"]:nMember)
pclObj is dynamic cLMyClass
aaAssociative is associative array of dynamic cLMyClass
pclObj = new cLMyClass
pclObj:nMember = 0
aaAssociative["Association"] = pclObj
pclObj:nMember = 2
Info(aaAssociative["Association"]:nMember)
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: MyAssociativeArray["Key"]:Member = 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…
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|