S-Lang Run-Time Library Reference: Version 1.4.0: Associative Array Functions

Next Previous Contents

## 2. Associative Array Functions

## 2.1 assoc_delete_key

Synopsis

  Delete a key from an Associative Array

Usage

  `assoc_delete_key (Assoc_Type a, String_Type k)`

Description

  The `assoc_delete_key` function deletes a key given by `k` from the associative array `a`. If the specified key does not exist in `a`, then this function has no effect.

See Also

  `assoc_key_exists, assoc_get_keys`

## 2.2 assoc_get_keys

Synopsis

  Return all the key names of an Associative Array

Usage

  `String_Type[] assoc_get_keys (Assoc_Type a)`

Description

  This function returns all the key names of an associative array `a` as an ordinary one dimensional array of strings. If the associative array contains no keys, an empty array will be returned.

Example

  The following function computes the number of keys in an associative array:

  > `

  >           define get_num_elements (a)
  >           {
  >              return length (assoc_get_keys (a));
  >           }

  > `

See Also

  `assoc_get_values, assoc_key_exists, assoc_delete_key, length`

## 2.3 assoc_get_values

Synopsis

  Return all the values of an Associative Array

Usage

  `Array_Type assoc_get_keys (Assoc_Type a)`

Description

  This function returns all the values in the associative array `a` as an array of proper type. If the associative array contains no keys, an empty array will be returned.

Example

  Suppose that `a` is an associative array of type `Integer_Type`, i.e., it was created via

  > `

  >           variable a = Assoc_Type[Integer_Type];

  > `

  The the following may be used to print the values of the array in ascending order:

  > `

  >           static define int_sort_fun (x, y)
  >           {
  >              return sign (x - y);
  >           }
  >           define sort_and_print_values (a)
  >           {
  >              variable i, v;

  >              v = assoc_get_values (a);
  >              i = array_sort (v, &int_sort_fun);
  >              v = v[i];
  >              foreach (v)
  >                {
  >                   variable vi = ();
  >                   () = fprintf (stdout, "%d\n", vi);
  >                }
  >           }

  > `

See Also

  `assoc_get_values, assoc_key_exists, assoc_delete_key, array_sort`

## 2.4 assoc_key_exists

Synopsis

  Check to see whether a key exists in an Associative Array

Usage

  `Integer_Type assoc_key_exists (Assoc_Type a, String_Type k)`

Description

  The `assoc_key_exists` function may be used to determine whether or not a specified key `k` exists in an associative array `a`. It returns `1` if the key exists, or `0` if it does not.

See Also

  `assoc_get_keys, assoc_get_values, assoc_delete_key`

Next Previous Contents