When size of a collection is unknown or the data space is sparse, an associative array is a better option. Associative arrays do not have any storage allocated until it is used, and the index expression is not restricted to integral expressions, but can be of any type.

An associative array implements a look-up table of the elements of its declared type. The data type to be used as an index serves as the lookup key and imposes an ordering.

  data_type array_identifier [ index_type ];
  int     array1 [int];       // An integer array with integer index
  int     array2 [string];     // An integer array with string index
  string  array3 [string];     // A string array with string index
  array1 = '{ 1 : 22,
              6 : 34 };
  array2 = '{ "Ross" : 100,
              "Joey" : 60 };
  array3 = '{ "Apples" : "Oranges",
              "Pears" : "44" };

Login to your free account to read more ...

Was this article helpful ?