2 assoc - create a new association array
11 This function returns an empty association array.
13 After A = assoc(), elements can be added to the association by
14 assignments of the forms
18 A[a_1, a_2, a_3] = v_3
19 A[a_1, a_2, a_3, a_4] = v_4
21 There are no restrictions on the values of the "indices" a_i or
24 After the above assignments, so long as no new values have been
25 assigned to A[a_i], etc., the expressions A[a_1], A[a_1, a_2], etc.
26 will return the values v_1, v_2, ...
28 Until A[a_1], A[a_1, a_2], ... are defined as described above, these
29 expressions return the null value.
31 Thus associations act like matrices except that different elements
32 may have different numbers (between 1 and 4 inclusive) of indices,
33 and these indices need not be integers in specified ranges.
35 Assignment of a null value to an element of an association does not
36 delete the element, but a later reference to that element will return
37 the null value as if the element is undefined.
39 The elements of an association are stored in a hash table for
40 quick access. The index values are hashed to select the correct
41 hash chain for a small sequential search for the element. The hash
42 table will be resized as necessary as the number of entries in
43 the association becomes larger.
45 The size function returns the number of elements in an association.
46 This size will include elements with null values.
48 Double bracket indexing can be used for associations to walk through
49 the elements of the association. The order that the elements are
50 returned in as the index increases is essentially random. Any
51 change made to the association can reorder the elements, this making
52 a sequential scan through the elements difficult.
54 The search and rsearch functions can search for an element in an
55 association which has the specified value. They return the index
56 of the found element, or a NULL value if the value was not found.
58 Associations can be copied by an assignment, and can be compared
59 for equality. But no other operations on associations have meaning,
63 ; A = assoc(); print A
66 ; A["zero"] = 0; A["one"] = 1; A["two"] = 2; A["three"] = 3;
67 ; A["smallest", "prime"] = 2;
74 ["smallest","prime"] = 2
83 isassoc, rsearch, search, size
85 ## Copyright (C) 1999 Landon Curt Noll
87 ## Calc is open software; you can redistribute it and/or modify it under
88 ## the terms of the version 2.1 of the GNU Lesser General Public License
89 ## as published by the Free Software Foundation.
91 ## Calc is distributed in the hope that it will be useful, but WITHOUT
92 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
93 ## or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
94 ## Public License for more details.
96 ## A copy of version 2.1 of the GNU Lesser General Public License is
97 ## distributed with calc under the filename COPYING-LGPL. You should have
98 ## received a copy with calc; if not, write to Free Software Foundation, Inc.
99 ## 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
101 ## @(#) $Revision: 30.1 $
102 ## @(#) $Id: assoc,v 30.1 2007/03/16 11:10:42 chongo Exp $
103 ## @(#) $Source: /usr/local/src/cmd/calc/help/RCS/assoc,v $
105 ## Under source code control: 1994/09/25 20:22:31
106 ## File existed as early as: 1994
108 ## chongo <was here> /\oo/\ http://www.isthe.com/chongo/
109 ## Share and enjoy! :-) http://www.isthe.com/chongo/tech/comp/calc/