in reply to Re^6: Combinations of lists, etc
in thread Combinations of lists to a hash
Further to LanX's reply: Consider the two statements
@ra = ({a=>1, b=>2}, {a=>1, b=>2});
and
@ra = ({a=>1, b=>2}) x 2;
In the first, two different anonymous array references are being constructed. In the second, a single anonymous array reference is constructed and then repeated twice.
c:\@Work\Perl\monks\tel2>perl -wMstrict -MData::Dump -le "my @ra = ({a=>1, b=>2}, {a=>1, b=>2}); print qq{@ra}; ;; @ra = ({a=>1, b=>2}) x 2; print qq{@ra}; " HASH(0x1555cdc) HASH(0x1555e50) HASH(0x1555e20) HASH(0x1555e20)
In
the do { ... } business with the 'fix' in the second dd instance just reflects the fact that a hash reference $a is built that needs to have its 'key2' key "fixed" later by a $a->{key2} = $a->{key1}; statement that makes the value of 'key2' the same as 'key1': they're the same reference. (Update: dd works by generatingc:\@Work\Perl\monks\tel2>perl -wMstrict -MData::Dump -le "my %hash; ;; @hash{ qw(key1 key2) } = ({a=>1, b=>2},{a=>1,b=>2}); dd \%hash; ;; @hash{ qw(key1 key2) } = ({a=>1, b=>2}) x 2; dd \%hash; " { key1 => { a => 1, b => 2 }, key2 => { a => 1, b => 2 } } do { my $a = { key1 => { a => 1, b => 2 }, key2 => 'fix' }; $a->{key2} = $a->{key1}; $a; }
Update: Clarified the update remark about dd per LanX's comment here. Thanks, LanX!
Give a man a fish: <%-{-{-{-<
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^8: Combinations of lists, etc
by LanX (Saint) on Oct 07, 2019 at 14:29 UTC | |
by AnomalousMonk (Archbishop) on Oct 07, 2019 at 17:48 UTC | |
by LanX (Saint) on Oct 07, 2019 at 18:40 UTC | |
|
Re^8: Combinations of lists, etc
by tel2 (Pilgrim) on Oct 07, 2019 at 21:19 UTC | |
by AnomalousMonk (Archbishop) on Oct 08, 2019 at 21:06 UTC | |
by tel2 (Pilgrim) on Oct 08, 2019 at 22:16 UTC | |
by AnomalousMonk (Archbishop) on Oct 08, 2019 at 22:41 UTC | |
by tel2 (Pilgrim) on Oct 08, 2019 at 23:02 UTC | |
by LanX (Saint) on Oct 08, 2019 at 21:29 UTC | |
by tel2 (Pilgrim) on Oct 08, 2019 at 22:01 UTC |