in reply to Re^8: Combinations of lists, etc
in thread Combinations of lists to a hash

> If I understand your question correctly, then yes.

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
    Thanks again, LanX.

    "The old approach is fine as long as you only read from the data structure without changing it..."

    OK, good.  I'll just be reading it, so I guess it will work for me.  

    I've just ran this which confirms it should work for me:

    perl -MData::Dump -e '@hash{(key1,key2)} = ({a=>1, b=>2}) x 2;dd \%has +h;print "hash{key1}{a}=$hash{key1}{a}\nhash{key1}{b}=$hash{key1}{b}\n +hash{key2}{a}=$hash{key2}{a}\nhash{key2}{b}=$hash{key2}{b}\n"' do { my $a = { key1 => { a => 1, b => 2 }, key2 => 'fix' }; $a->{key2} = $a->{key1}; $a; } hash{key1}{a}=1 hash{key1}{b}=2 hash{key2}{a}=1 hash{key2}{b}=2
    Thanks again for all your help.