1Tie::IxHash(3) User Contributed Perl Documentation Tie::IxHash(3)
2
3
4
6 Tie::IxHash - ordered associative arrays for Perl
7
9 # simple usage
10 use Tie::IxHash;
11 tie HASHVARIABLE, Tie::IxHash [, LIST];
12
13 # OO interface with more powerful features
14 use Tie::IxHash;
15 TIEOBJECT = Tie::IxHash->new( [LIST] );
16 TIEOBJECT->Splice( OFFSET [, LENGTH [, LIST]] );
17 TIEOBJECT->Push( LIST );
18 TIEOBJECT->Pop;
19 TIEOBJECT->Shift;
20 TIEOBJECT->Unshift( LIST );
21 TIEOBJECT->Keys( [LIST] );
22 TIEOBJECT->Values( [LIST] );
23 TIEOBJECT->Indices( LIST );
24 TIEOBJECT->Delete( [LIST] );
25 TIEOBJECT->Replace( OFFSET, VALUE, [KEY] );
26 TIEOBJECT->Reorder( LIST );
27 TIEOBJECT->SortByKey;
28 TIEOBJECT->SortByValue;
29 TIEOBJECT->Length;
30
32 This Perl module implements Perl hashes that preserve the order in
33 which the hash elements were added. The order is not affected when
34 values corresponding to existing keys in the IxHash are changed. The
35 elements can also be set to any arbitrary supplied order. The familiar
36 perl array operations can also be performed on the IxHash.
37
38 Standard "TIEHASH" Interface
39 The standard "TIEHASH" mechanism is available. This interface is
40 recommended for simple uses, since the usage is exactly the same as
41 regular Perl hashes after the "tie" is declared.
42
43 Object Interface
44 This module also provides an extended object-oriented interface that
45 can be used for more powerful operations with the IxHash. The
46 following methods are available:
47
48 FETCH, STORE, DELETE, EXISTS
49 These standard "TIEHASH" methods mandated by Perl can be used
50 directly. See the "tie" entry in perlfunc(1) for details.
51
52 Push, Pop, Shift, Unshift, Splice
53 These additional methods resembling Perl functions are
54 available for operating on key-value pairs in the IxHash. The
55 behavior is the same as the corresponding perl functions,
56 except when a supplied hash key already exists in the hash. In
57 that case, the existing value is updated but its order is not
58 affected. To unconditionally alter the order of a supplied
59 key-value pair, first "DELETE" the IxHash element.
60
61 Keys Returns an array of IxHash element keys corresponding to the
62 list of supplied indices. Returns an array of all the keys if
63 called without arguments. Note the return value is mostly only
64 useful when used in a list context (since perl will convert it
65 to the number of elements in the array when used in a scalar
66 context, and that may not be very useful).
67
68 If a single argument is given, returns the single key
69 corresponding to the index. This is usable in either scalar or
70 list context.
71
72 Values Returns an array of IxHash element values corresponding to the
73 list of supplied indices. Returns an array of all the values
74 if called without arguments. Note the return value is mostly
75 only useful when used in a list context (since perl will
76 convert it to the number of elements in the array when used in
77 a scalar context, and that may not be very useful).
78
79 If a single argument is given, returns the single value
80 corresponding to the index. This is usable in either scalar or
81 list context.
82
83 Indices Returns an array of indices corresponding to the supplied list
84 of keys. Note the return value is mostly only useful when used
85 in a list context (since perl will convert it to the number of
86 elements in the array when used in a scalar context, and that
87 may not be very useful).
88
89 If a single argument is given, returns the single index
90 corresponding to the key. This is usable in either scalar or
91 list context.
92
93 Delete Removes elements with the supplied keys from the IxHash.
94
95 Replace Substitutes the IxHash element at the specified index with the
96 supplied value-key pair. If a key is not supplied, simply
97 substitutes the value at index with the supplied value. If an
98 element with the supplied key already exists, it will be
99 removed from the IxHash first.
100
101 Reorder This method can be used to manipulate the internal order of the
102 IxHash elements by supplying a list of keys in the desired
103 order. Note however, that any IxHash elements whose keys are
104 not in the list will be removed from the IxHash.
105
106 Length Returns the number of IxHash elements.
107
108 SortByKey
109 Reorders the IxHash elements by textual comparison of the keys.
110
111 SortByValue
112 Reorders the IxHash elements by textual comparison of the
113 values.
114
116 use Tie::IxHash;
117
118 # simple interface
119 $t = tie(%myhash, Tie::IxHash, 'a' => 1, 'b' => 2);
120 %myhash = (first => 1, second => 2, third => 3);
121 $myhash{fourth} = 4;
122 @keys = keys %myhash;
123 @values = values %myhash;
124 print("y") if exists $myhash{third};
125
126 # OO interface
127 $t = Tie::IxHash->new(first => 1, second => 2, third => 3);
128 $t->Push(fourth => 4); # same as $myhash{'fourth'} = 4;
129 ($k, $v) = $t->Pop; # $k is 'fourth', $v is 4
130 $t->Unshift(neg => -1, zeroth => 0);
131 ($k, $v) = $t->Shift; # $k is 'neg', $v is -1
132 @oneandtwo = $t->Splice(1, 2, foo => 100, bar => 101);
133
134 @keys = $t->Keys;
135 @values = $t->Values;
136 @indices = $t->Indices('foo', 'zeroth');
137 @itemkeys = $t->Keys(@indices);
138 @itemvals = $t->Values(@indices);
139 $t->Replace(2, 0.3, 'other');
140 $t->Delete('second', 'zeroth');
141 $len = $t->Length; # number of key-value pairs
142
143 $t->Reorder(reverse @keys);
144 $t->SortByKey;
145 $t->SortByValue;
146
148 You cannot specify a negative length to "Splice". Negative indexes are
149 OK, though.
150
151 Indexing always begins at 0 (despite the current $[ setting) for all
152 the functions.
153
155 Addition of elements with keys that already exist to the end of the
156 IxHash must be controlled by a switch.
157
158 Provide "TIEARRAY" interface when it stabilizes in Perl.
159
160 Rewrite using XSUBs for efficiency.
161
163 Gurusamy Sarathy gsar@umich.edu
164
165 Copyright (c) 1995 Gurusamy Sarathy. All rights reserved. This program
166 is free software; you can redistribute it and/or modify it under the
167 same terms as Perl itself.
168
170 Version 1.22 27 February 2010
171
173 perl(1)
174
175
176
177perl v5.12.0 2010-02-27 Tie::IxHash(3)