Tie::Hash::MultiValue - store multiple values per key
use Tie::Hash::MultiValue; tie %hash, 'Tie::Hash::Multivalue'; $hash{'foo'} = 'one'; $hash{'bar'} = 'two'; $hash{'bar'} = 'three'; my @values = @{$hash{'foo'}}; # @values = ('one'); my @more = @{$hash{'bar'}}; # @more = ('two', 'three'); my @nothing = $hash{'baz'}; # undefined if nothing there # You can save multiple values at once: $hash{'more'} = ('fee','fie', 'foe', 'fum'); my @giant_words = @{$hash{'more'}}; # You can tie an anonymous hash as well. my $hash = {}; tie %$hash, 'Tie::Hash::MultiValue'; $hash->{'sample'} = 'one'; $hash->{'sample'} = 'two'; # $hash->{'sample'} now contains ['one','two']
Tie::Hash::Multivalue allows you to have hashes which store their values in anonymous arrays, appending any new value to the already-existing ones.
Tie::Hash::Multivalue
This means that you can store as many items as you like under a single key, and access them all at once by accessing the value stored under the key.
See the synopsis for a typical usage.
None currently known.
Contact the author for support.
Joe McMahon CPAN ID: MCMAHON mcmahon@ibiblio.org http://ibiblio.org/mcmahon
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
The full text of the license can be found in the LICENSE file included with this module.
Tie::Hash, perl(1), Perl Cookbook (1st version) recipe 13.15, program 13-5.
This class is a subclass of Tie::ExtraHash; it needs to override the TIEHASH method to save the instance data (in $self->[1]), and the STORE method to actually save the values in an anonymous array.
Tie::ExtraHash
TIEHASH
STORE
If the 'unique' argument is supplied, we check to see if it supplies a subroutine reference to be used to compare items. If it does, we store that reference in the object describing this tie; if not, we supply a function which simply uses 'eq' to test for equality.
This funtion will receive two scalar arguments. No assumption is made about whether or not either argument is defined, nor whether these are simple scalars or references. You can make any of these assumptions if you choose, but you are responsible for checking your input.
You can perform whatever tests you like in your routine; you should return a true value if the arguments are determined to be equal, and a false one if they are not.
Push the value(s) supplied onto the list of values stored here. The anonymous array is created automatically if it doesn't yet exist.
If the 'unique' argument was supplied at the time the hash was tied, we will use the associated function (either yours, if you supplied one; or ours, if you didn't) and only add the item or items that are not present.
To install Tie::Hash::MultiValue, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Tie::Hash::MultiValue
CPAN shell
perl -MCPAN -e shell install Tie::Hash::MultiValue
For more information on module installation, please visit the detailed CPAN module installation guide.