Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: (another) HoH question

by 7stud (Deacon)
on Jun 23, 2012 at 19:37 UTC ( #978004=note: print w/ replies, xml ) Need Help??


in reply to (another) HoH question

Hash: 123->abc 456->abc 456->xyz 789->abc

But...

use strict; use warnings; use 5.010; use Data::Dumper; my %hash = ( 123 => 'abc', 456 => 'abc', 456 => 'xyz', 789 => 'abc', ); print Dumper(\%hash); --output:-- $VAR1 = { '456' => 'xyz', '123' => 'abc', '789' => 'abc' };

The corollary to "keys can have only one value" is "a hash cannot have duplicate keys". If a hash could look like this:

Hash: 123->abc 456->abc 456->xyz 789->abc

...how would perl know what value to return when you said, "Give me the value associated with the key '456'?


Comment on Re: (another) HoH question
Select or Download Code
Re^2: (another) HoH question
by muba (Priest) on Jun 23, 2012 at 19:53 UTC

    "a hash cannot have duplicate keys". If a hash could look like this: (...snip...) ...how would perl know what value to return when you said, "Give me the value associated with the key '456'?

    True. But a hash value can be an array ref, so in a way you can associate multiple values to a single key. Which, I think, is what is needed here.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://978004]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (6)
As of 2014-10-31 11:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (216 votes), past polls