Tie::CPHash - Case preserving but case insensitive hash table |
Tie::CPHash - Case preserving but case insensitive hash table
require Tie::CPHash; tie %cphash, 'Tie::CPHash';
$cphash{'Hello World'} = 'Hi there!'; printf("The key `%s' was used to store `%s'.\n", tied(%cphash)->key('HELLO WORLD'), $cphash{'HELLO world'});
The Tie::CPHash provides a hash table that is case preserving but case insensitive. This means that
$cphash{KEY} $cphash{key} $cphash{Key} $cphash{keY}
all refer to the same entry. Also, the hash remembers which form of
the key was last used to store the entry. The keys
and each
functions will return the key that was used to set the value.
An example should make this clear:
tie %h, 'Tie::CPHash'; $h{Hello} = 'World'; print $h{HELLO}; # Prints 'World' print keys(%h); # Prints 'Hello' $h{HELLO} = 'WORLD'; print $h{hello}; # Prints 'WORLD' print keys(%h); # Prints 'HELLO'
The additional key
method lets you fetch the case of a specific key:
# When run after the previous example, this prints 'HELLO': print tied(%h)->key('Hello');
(The tied
function returns the object that %h
is tied to.)
If you need a case insensitive hash, but don't need to preserve case,
just use $hash{lc $key}
instead of $hash{$key}
. This has a lot
less overhead than Tie::CPHash.
Christopher J. Madsen <chris_madsen@geocities.com>
Tie::CPHash - Case preserving but case insensitive hash table |