<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"> <HTML> <HEAD> <META NAME="GENERATOR" CONTENT="SGML-Tools 1.0.9"> <TITLE> A Guide to the S-Lang Language: Associative Arrays</TITLE> <LINK HREF="slang-13.html" REL=next> <LINK HREF="slang-11.html" REL=previous> <LINK HREF="slang.html#toc12" REL=contents> </HEAD> <BODY> <A HREF="slang-13.html">Next</A> <A HREF="slang-11.html">Previous</A> <A HREF="slang.html#toc12">Contents</A> <HR> <H2><A NAME="s12">12. Associative Arrays</A></H2> <P> <P>An associative array differs from an ordinary array in the sense that its size is not fixed and that is indexed by a string, called the <EM>key</EM>. For example, consider: <BLOCKQUOTE><CODE> <PRE> variable A = Assoc_Type [Integer_Type]; A["alpha"] = 1; A["beta"] = 2; A["gamma"] = 3; </PRE> </CODE></BLOCKQUOTE> Here, <CODE>A</CODE> represents an associative array of integers (<CODE>Integer_Type</CODE>) and three keys have been added to the array. <P>As the example suggests, an associative array may be created using one of the following forms: <BLOCKQUOTE><CODE> Assoc_Type [<EM>type</EM>] Assoc_Type [<EM>type</EM>, <EM>default-value</EM>] Assoc_Type [] </CODE></BLOCKQUOTE> The last form returns an associative array of <CODE>Any_Type</CODE> objects allowing any type of object to may be stored in the array. <P>The form involving a <EM>default-value</EM> is useful for associating a default value for non-existent array members. This feature is explained in more detail below. <P>There are several functions that are specially designed to work with associative arrays. These include: <UL> <LI> <CODE>assoc_get_keys</CODE>, which returns an ordinary array of strings containing the keys in the array. </LI> <LI> <CODE>assoc_get_values</CODE>, which returns an ordinary array of the values of the associative array. </LI> <LI> <CODE>assoc_key_exists</CODE>, which can be used to determine whether or not a key exists in the array. </LI> <LI> <CODE>assoc_delete_key</CODE>, which may be used to remove a key (and its value) from the array.</LI> </UL> <P>To illustrate the use of an associative array, consider the problem of counting the number of repeated occurrences of words in a list. Let the word list be represented as an array of strings given by <CODE>word_list</CODE>. The number of occurrences of each word may be stored in an associative array as follows: <BLOCKQUOTE><CODE> <PRE> variable a, word; a = Assoc_Type [Integer_Type]; foreach (word_list) { word = (); if (0 == assoc_key_exists (a, word)) a[word] = 0; a[word]++; % same as a[word] = a[word] + 1; } </PRE> </CODE></BLOCKQUOTE> Note that <CODE>assoc_key_exists</CODE> was necessary to determine whether or not a word was already added to the array in order to properly initialize it. However, by creating the associative array with a default value of <CODE>0</CODE>, the above code may be simplified to <BLOCKQUOTE><CODE> <PRE> variable a, word; a = Assoc_Type [Integer_Type, 0]; foreach (word_list) { word = (); a[word]++; } </PRE> </CODE></BLOCKQUOTE> <P> <P> <P> <HR> <A HREF="slang-13.html">Next</A> <A HREF="slang-11.html">Previous</A> <A HREF="slang.html#toc12">Contents</A> </BODY> </HTML>