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

Sorry LanX - I got mixed up.
I've updated my last post now with your code.

Replies are listed 'Best First'.
Re^5: Combinations of lists, etc
by LanX (Saint) on Oct 05, 2019 at 13:57 UTC
    Please try to understand how the code works, especially the loop with the two nested maps °

    Your desired changes are trivial then.

    I did the demo in the debugger to help you experiment. Please use strict and warnings when translating into a script.

    We are glad to answer your questions.

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

    °) could also be translated into a 3 level for loop if it's easier to understand for you.

      Thanks LanX.

      I'm trying to modify your code to handle update #2 in my original post, i.e. this line:

      @hash{@keys} = ('value2') x @keys
      This test seems to work as I want it to:
      perl -MData::Dump -e '@hash{(key1,key2)} = ({a=>1, b=>2},{a=>1,b=>2}); +dd \%hash' { key1 => { a => 1, b => 2 }, key2 => { a => 1, b => 2 } }
      But when I try to multiply the values out, it doesn't:
      perl -MData::Dump -e '@hash{(key1,key2)} = ({a=>1, b=>2}) x 2;dd \%has +h' do { my $a = { key1 => { a => 1, b => 2 }, key2 => 'fix' }; $a->{key2} = $a->{key1}; $a; }
      And the debugger didn't shed any light for me, either.
      What am I doing wrong, please?  And what's that 'fix' thing about?

        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

        c:\@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; }
        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 generating source code that | source code text that, when run thru eval, will reproduce the structure of the data being dumped.)

        Update: Clarified the update remark about  dd per LanX's comment here. Thanks, LanX!


        Give a man a fish:  <%-{-{-{-<

        > What am I doing wrong, please?

        The output looks right to me. You are assigning the same hashref again.

        Do you need the hashes to different instances with same content?

        Then you need to loop over all keys and always assign a new copy { %$hashref }

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery FootballPerl is like chess, only without the dice