in reply to duplicate keys on hash
Furthermore, Perl has a neat feature called, of all things, “autovivification” which makes this sort of thing very easy to express. For example:
... will in one step create a hash-key for 'foo' if it does not yet exist, and cause that bucket to contain an arrayref, and to push 'bar' onto that array. So, in just one step, “the right thing” happens.my $hash = {}; push @{$hash->{'foo'}}, 'bar';
Each element of the resulting hash will therefore contain an arrayref, even if that arrayref contains only one entry. In this way, an arbitrary number of values may be stored. The same auto-vification trick can be used to construct other structures, as well.
Edited to fix the braces.
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: duplicate keys on hash
by choroba (Cardinal) on Feb 11, 2015 at 13:18 UTC | |
by Anonymous Monk on Feb 11, 2015 at 19:57 UTC |