1Tie::IxHash(3)        User Contributed Perl Documentation       Tie::IxHash(3)
2
3
4

NAME

6       Tie::IxHash - ordered associative arrays for Perl
7

SYNOPSIS

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

DESCRIPTION

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
115       Clear   Resets the IxHash to its pristine state: with no elements at
116               all.
117

EXAMPLE

119           use Tie::IxHash;
120
121           # simple interface
122           $t = tie(%myhash, 'Tie::IxHash', 'a' => 1, 'b' => 2);
123           %myhash = (first => 1, second => 2, third => 3);
124           $myhash{fourth} = 4;
125           @keys = keys %myhash;
126           @values = values %myhash;
127           print("y") if exists $myhash{third};
128
129           # OO interface
130           $t = Tie::IxHash->new(first => 1, second => 2, third => 3);
131           $t->Push(fourth => 4); # same as $myhash{'fourth'} = 4;
132           ($k, $v) = $t->Pop;    # $k is 'fourth', $v is 4
133           $t->Unshift(neg => -1, zeroth => 0);
134           ($k, $v) = $t->Shift;  # $k is 'neg', $v is -1
135           @oneandtwo = $t->Splice(1, 2, foo => 100, bar => 101);
136
137           @keys = $t->Keys;
138           @values = $t->Values;
139           @indices = $t->Indices('foo', 'zeroth');
140           @itemkeys = $t->Keys(@indices);
141           @itemvals = $t->Values(@indices);
142           $t->Replace(2, 0.3, 'other');
143           $t->Delete('second', 'zeroth');
144           $len = $t->Length;     # number of key-value pairs
145
146           $t->Reorder(reverse @keys);
147           $t->SortByKey;
148           $t->SortByValue;
149

BUGS

151       You cannot specify a negative length to "Splice". Negative indexes are
152       OK, though.
153

NOTE

155       Indexing always begins at 0 (despite the current $[ setting) for all
156       the functions.
157

TODO

159       Addition of elements with keys that already exist to the end of the
160       IxHash must be controlled by a switch.
161
162       Provide "TIEARRAY" interface when it stabilizes in Perl.
163
164       Rewrite using XSUBs for efficiency.
165

AUTHOR

167       Gurusamy Sarathy        gsar@umich.edu
168
169       Copyright (c) 1995 Gurusamy Sarathy. All rights reserved.  This program
170       is free software; you can redistribute it and/or modify it under the
171       same terms as Perl itself.
172

VERSION

174       Version 1.23
175

SEE ALSO

177       perl(1)
178
179
180
181perl v5.34.0                      2022-01-21                    Tie::IxHash(3)
Impressum