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

Thanks for that, AnomalousMonk.

"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."

So how can we use this concise & convenient "x 2" syntax to generate the former?  I've just proposed a workaround here, but maybe there's a cleaner method.

Update: It looks as if I don't actually need an alternative, because the later should work for my needs, since I'll only be reading it, according to LanX.  But I'm still interested if you know of a concise efficient alternative, which would allow reading & writing.

Replies are listed 'Best First'.
Re^9: Combinations of lists, etc
by AnomalousMonk (Archbishop) on Oct 08, 2019 at 21:06 UTC
    "In the first, two different anonymous array references are being constructed. In the second, ..."

    So how can we use this concise & convenient "x 2" syntax to generate the former?

    I think you understand this already, so I'll answer just for the sake of completeness: You can't. The  x operator (see perlop) makes exact copies either of the elements of a list, the copies of which form another list, or of a string | a scalar, which is a string or stringized "thing" (number, reference, whatever), the copies of which are then contatenated together. And it's not possible to get different things if you're making exact copies of a thing (if you're doing it right).


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

      Well said, LanX.

      So would it be true to say that when taking such "exact" copies of a string, you can modify the copy without affecting the original, but this is not the case when taking such "exact" copies of things like hash keys & values, because the copy will be some kind of reference to the original?

        Well said, LanX.

        Well, it wasn't LanX, but...

        ... when taking such "exact" copies of a string, you can modify the copy without affecting the original ...

        Yes, because in the case of x, scalars (e.g., strings) are copied (and concatenated) by value, so the copy is independent.

        ... this is not the case when taking such "exact" copies of things like hash keys ...

        Hash keys are strings (scalars).

        ... & values, because the copy will be some kind of reference to the original?

        The value of a hash key/value pair (or item of an array (update: or list)) is always a scalar. A scalar can be a reference. (A reference is always a scalar.) Operator  x copies things exactly, but two or however many exact copies of a given reference always point to a single thing: the "referent". A referent may be referenced by any number of references. (Update: See reference, perlref, perlreftut, and perldsc.)


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

Re^9: Combinations of lists, etc
by LanX (Saint) on Oct 08, 2019 at 21:29 UTC
    > But I'm still interested if you know of a concise efficient alternative, which would allow reading & writing.

    We already showed you:

    $hash{$_} = { %$value } for @keys;

    or

    $hash{$_} = { %value } for @keys;

    The only concise alternative which comes to mind is

    @hash{ @keys } = map { { %value } } @keys

    But it's neither shorter nor easier to read.

    Cheers Rolf

    update

    But honestly you should stick to syntax you understand, even if it takes some lines more.

      Thanks for that, again, LanX.