Associative Arrays

All CXL arrays are associative, i.e., indexed by string values.

Example

This example accumulates the total cost of each cat and each dog from a Pet Store's Inventory report and stores the cost information in the cost associative array. This is done by searching the current input line for the string Cat and if found adds the cost of the cat (in field 3) to the cost array using "Cat" as the subscript. It then searches the current input line for the string Dog and if found adds the cost of the dog (in field 3) to the cost array using "Dog" as the subscript.

/Cat/ { cost["Cat"] += $4 } /Dog/ { cost ["Dog"] += $6 }

If cost[Cat] had been written instead of cost["Cat"], the expression would have used the value of the variable Cat as the subscript. Since the variable is uninitialized, the values would have accumulated in cost[""]. Note that even though CXL arrays are associative, that does not restrict the user from using numbers as subscripts.

Last modified date: 01/25/2022