Beefy Boxes and Bandwidth Generously Provided by pair Networks Bob
Syntactic Confectionery Delight
 
PerlMonks  

Multiple Hash Keys Module

by arunhorne (Pilgrim)
on May 23, 2003 at 14:25 UTC ( #260360=perlquestion: print w/ replies, xml ) Need Help??
arunhorne has asked for the wisdom of the Perl Monks concerning the following question:

Monks

I want to create hash that maps a single key to multiple values. My current way of doing this is to use and array reference:

my %hash = ("key"=>["value1","value2"]);

However, I was wondering if anyone knows of a module that provides this sort of behaviour whilst abstracting the specifics of creating array refs etc?

____________
Arun

Comment on Multiple Hash Keys Module
Download Code
Re: Multiple Hash Keys Module
by Zaxo (Archbishop) on May 23, 2003 at 14:36 UTC

    With mild quibbles on quoting, what you have is fine. It's simple and if it does what you want it would be hard to improve on. If anything, it's a little more abstracted than some might like. I can't think of any modular sugar that I would prefer to your code.

    As for quoting, the '=>' form of comma stringifies its left hand side, so key can remain unquoted. If the value strings contain no variables to interpolate, single quotes are preferred, and if thay have no whitespace, qw() might be handy.

    After Compline,
    Zaxo

Re: Multiple Hash Keys Module
by arthas (Hermit) on May 23, 2003 at 15:06 UTC
    The Perl solution is, IMHO, very immediate and, also, very well documented in the pod pages. You probably need nothing more abstract.

    Michele.
Re: Multiple Hash Keys Module
by artist (Parson) on May 23, 2003 at 15:08 UTC
    Zaxo has pointed you in the right direction, but if for any means, if you need 'modular sugar',you can use something like this with better design.
    package Hash; sub new { my $class = shift; my $self = {}; bless $self,$class; } sub addKey { my $self = shift; my $key = shift; $self->{$key}; } sub printKeys { my $self = shift; foreach (keys %{$self}){ print "$_\n"; } } sub addValues { my $self = shift; my $key = shift; push @{$self->{$key}}, @_; } sub printValues { my $self = shift; my $key = shift; print "$key\t"; foreach (@{$self->{$key}}){ print "$_\t"; } print "\n"; } package main; my $hash = new Hash; $hash->addKey('fruit'); $hash->addValues('fruit', 'apple'); $hash->addValues('fruit','orange'); $hash->printValues('fruit'); $hash->addKey('operating system'); $hash->addValues('operating system', 'Linux','Windows XP'); $hash->printValues('operating system');
    artist
Re: Multiple Hash Keys Module
by gmpassos (Priest) on May 23, 2003 at 16:28 UTC
    Keep It Simple! Using the Array ref is better.

    But maybe this can help you: Object::MultiType

    Graciliano M. P.
    "The creativity is the expression of the liberty".

Re: Multiple Hash Keys Module
by arunhorne (Pilgrim) on May 27, 2003 at 11:50 UTC

    Further to this thread I have written an object to satisfy the task set out for this node, more details here

    ____________
    Arun

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://260360]
Approved by broquaint
Front-paged by halley
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (8)
As of 2014-04-20 00:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (485 votes), past polls