Next: , Previous: Array Data Types, Up: Array Manipulation


16.4.10.2 Array Functions

The following functions relate to individual array elements.

awk_bool_t get_element_count(awk_array_t a_cookie, size_t *count);
For the array represented by a_cookie, return in *count the number of elements it contains. A subarray counts as a single element. Return false if there is an error.
awk_bool_t get_array_element(awk_array_t a_cookie,
const awk_value_t *const index,
awk_valtype_t wanted,
awk_value_t *result);
For the array represented by a_cookie, return in *result the value of the element whose index is index. wanted specifies the type of value you wish to retrieve. Return false if wanted does not match the actual type or if index is not in the array (see Table 16.1).

The value for index can be numeric, in which case gawk converts it to a string. Using non-integral values is possible, but requires that you understand how such values are converted to strings (see Conversion); thus using integral values is safest.

As with all strings passed into gawk from an extension, the string value of index must come from malloc(), and gawk releases the storage.

awk_bool_t set_array_element(awk_array_t a_cookie,
const awk_value_t *const index,
const awk_value_t *const value);
In the array represented by a_cookie, create or modify the element whose index is given by index. The ARGV and ENVIRON arrays may not be changed.
awk_bool_t set_array_element_by_elem(awk_array_t a_cookie,
awk_element_t element);
Like set_array_element(), but take the index and value from element. This is a convenience macro.
awk_bool_t del_array_element(awk_array_t a_cookie,
const awk_value_t* const index);
Remove the element with the given index from the array represented by a_cookie. Return true if the element was removed, or false if the element did not exist in the array.

The following functions relate to arrays as a whole:

awk_array_t create_array();
Create a new array to which elements may be added. See Creating Arrays, for a discussion of how to create a new array and add elements to it.
awk_bool_t clear_array(awk_array_t a_cookie);
Clear the array represented by a_cookie. Return false if there was some kind of problem, true otherwise. The array remains an array, but after calling this function, it has no elements. This is equivalent to using the delete statement (see Delete).
awk_bool_t flatten_array(awk_array_t a_cookie, awk_flat_array_t **data);
For the array represented by a_cookie, create an awk_flat_array_t structure and fill it in. Set the pointer whose address is passed as data to point to this structure. Return true upon success, or false otherwise. See Flattening Arrays, for a discussion of how to flatten an array and work with it.
awk_bool_t release_flattened_array(awk_array_t a_cookie,
awk_flat_array_t *data);
When done with a flattened array, release the storage using this function. You must pass in both the original array cookie, and the address of the created awk_flat_array_t structure. The function returns true upon success, false otherwise.