in reply to Re^8: Combinations of lists, etc
in thread Combinations of lists to a hash
I'm afraid you don't.
The old approach is fine as long as you only read from the data structure without changing it...
(The fact that the output of Data::Dump is confusing you doesn't mean it doesn't "work".)
DB<82> $hash{$_} = $value for @keys; DB<83> x \%hash 0 HASH(0x3598330) 'Prefix1=A:b:1' => HASH(0x35a48e8) 'a' => 1 'b' => 2 'Prefix1=A:b:2' => HASH(0x35a48e8) -> REUSED_ADDRESS 'Prefix1=A:c:1' => HASH(0x35a48e8) -> REUSED_ADDRESS 'Prefix1=A:c:2' => HASH(0x35a48e8) -> REUSED_ADDRESS DB<84> p $hash{'Prefix1=A:b:1'}{a} 1 DB<85> p $hash{'Prefix1=A:b:1'}{b} 2 DB<86> p $hash{'Prefix1=A:c:2'}{b} # reading works! 2 DB<87>
but once you try to add more data it'll be mirrored, because all values of the outer hash point to the same inner hash.
(Like a symlinked directory if this helps)
DB<87> p $hash{'Prefix1=A:c:2'}{X}=42 # new entry 42 DB<88> x \%hash 0 HASH(0x3598330) 'Prefix1=A:b:1' => HASH(0x35a48e8) # appears everywhere 'X' => 42 'a' => 1 'b' => 2 'Prefix1=A:b:2' => HASH(0x35a48e8) -> REUSED_ADDRESS 'Prefix1=A:c:1' => HASH(0x35a48e8) -> REUSED_ADDRESS 'Prefix1=A:c:2' => HASH(0x35a48e8) -> REUSED_ADDRESS DB<89>
So in case you want to store more data inside the inner hashes, which are supposed to be different, you have to clone the hashes.
> what would be wrong with this kind of strategy:
Sorry, I don't have even time to discuss all the traps here.
Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery
FootballPerl is like chess, only without the dice
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^10: Combinations of lists, etc
by tel2 (Pilgrim) on Oct 07, 2019 at 22:23 UTC |