Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

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
[ambrus]: Now it has five people instead of three, some sort of call number ticket system where people wait a lot for their food to get ready (it's the same kinds of grilled meat and fish on the same equipment, it won't actually fry slower),
[ambrus]: it's slow as hell, and the food costs significantly more.
[ambrus]: We no longer eat there.
LanX Everybody quotes it, some people read it, and a few people go by it
[ambrus]: LanX: why would I read it? isn't it a book for managers? I don't want to be a manager.
[ambrus]: I don't quote it either.
[erix]: it's really a software book, albeit about experiences from the 60s-80s. People haven't changed all that much though.
[erix]: a software project book, is probably more exact
[ambrus]: Yes, but I'm not managing software teams either.
[ambrus]: I'm not only not a restaurant manager, but also not a software manager.

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (16)
As of 2017-09-22 13:53 GMT
Find Nodes?
    Voting Booth?
    During the recent solar eclipse, I:

    Results (264 votes). Check out past polls.