Monks

Further to my ramblings here about creating a hash that has multiple values per key I have gone the whole hog and written an object to perform this function having been advised that no such object currently exists. I don't think that it is comprehensive enough to be placed on CPAN but I do think that it may be useful to other users so I am posting it here.

Example usage and other details are included in the pod docs at the top of the file. Any constructive suggestions greatly appreciated. Code below:

=head1 NAME Datastruct::MultiMap =head1 DESCRIPTION This object provides a simple front to a hash mapping keys to arrays o +f values thus allowing multiple values for a single key. =head1 AUTHOR Arun Horne (arun@simbios.net) =head1 VERSION HISTORY 1.0 - Initial version (27 May 2003) =head1 USAGE use strict; use warnings; use Datastruct::MultiMap; use Data::Dumper; # Create a multimap object my $map = Datastruct::MultiMap->new(); # Add some keys $map->put('K1','V1'); $map->put('K1','V2'); $map->put('K2','V3'); $map->put('K3','V4'); print Dumper $map; # Get values my @values = $map->get('K1'); print Dumper \@values; # Remove keys $map->remove('K1'); print Dumper $map; # Can still access as a normal hash if you want print join(",", sort keys %$map), "\n"; =cut package Datastruct::MultiMap; use strict; use warnings; # # Creates an empty multimap. # sub new() { my $class = shift; my $self = {}; bless $self, $class; return $self; } # # Stores a (key, value) pair into the map # sub put() { my ($self, $key, $value) = @_; unless (exists $self->{$key}) { $self->{$key} = []; } push @{$self->{$key}}, $value; } # # Returns an array of all the values mapped by the specific key # sub get() { my ($self, $key) = @_; return @{$self->{$key}}; } # # Removes all of the values mapped by the specified key. # sub remove() { my ($self, $key) = @_; delete $self->{$key}; } # Package must return true 1;
____________
Arun

In reply to Object to Map Multiple Values to a Single Key by arunhorne

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.