Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

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:


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?


Replies are listed 'Best First'.
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,

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');
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.

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


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://260360]
Approved by broquaint
Front-paged by halley
[Discipulus]: ovedpo15 A subroutine may be called using an explicit & prefix. The & is optional in modern Perl... see perlsub in docs and search for ampersand
[Discipulus]: it has some, even dangerous, implication
[Discipulus]: I still use but I also attract many critics for this: I use when I call subs defined in the very same file, just to recognize them. You can avoid (but sometimes is needed)
[marto]: believe it or not this is a SPAM account :P
[Discipulus]: it seems a legitimate one.. grin ..

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (10)
As of 2018-05-22 12:14 GMT
Find Nodes?
    Voting Booth?