What am I missing?
Two answers among many possible answers are:
You're missing a colon.
Specifically, you can change:
my ( %H, %G ) = GetHashes ;
to:
my ( %H, %G ) := GetHashes ;
^
You're missing @p6steve's answer.
Specifically, you can change:
my ( %H, %G ) = GetHashes ;
to:
my ( $H, $G ) = GetHashes ;
The summary is that Raku supports multiple ways to make some storage location(s) either "contain" some data or "be a reference to" some data. You were using "list assignment" but need to use one of the alternatives, such as our answers.
Assignment (=
)
Assignment means copying data to some storage location(s) that end up "containing" the copied data.[1]
If you're using list assignment, then the more items in the list, the more bytes get copied. If you've got a million items, it's unlikely you want to assign them using list assignment. And there can be other reasons to not want to use assignment, as you have discovered.
Consider, for example, my @array1 = 1, 2
. This code:
Constructs a List
of two elements -- (1, 2)
-- as the data to be assigned/copied;
Constructs a new empty Array
and "binds" this new array to the "symbol" (variable name) @array1
;
Iterates the List
's elements and, for each:
3.1 Adds a new corresponding empty "container" (a Scalar
) to the target array, ready to receive some scalar data;
3.2 Copies a scalar (single) value from the List
into the new Scalar
container in the array.
Note how this ends up creating three storage locations at run-time: an array, and two individual Scalar
s as the first two indexable elements of that array. All three of these storage locations get updated, with the array getting two Scalar
s added to it, and the two Scalar
s receiving the two values copied from the list.
Binding (:=
)
Binding means copying references (pointers) to data. The data itself is not copied. If there are, say, two references to data, then very little gets copied, no matter how large the data is. One way to stick to copying references, rather than the data being referred to, is to use binding.
Consider, for example my @array2 := 1, 2
. This code:
Constructs a List
of two elements (1, 2)
;
Binds a reference to the List
to the "symbol" (variable name) @array2
.
Note how, unlike the assignment example above, this binding example ends up with just one storage location (@array2
) on the left of the :=
being updated, corresponding to the right being treated as a single value (a List
).
There can be multiple storage locations on the left of a binding operation. Consider your example, my ( %H, %G ) := GetHashes;
. This code:
Evaluates GetHashes
, returning a List
of two hashes;
Iterates the list of values/references on the right of the :=
, binding each in turn to the list of target storage locations on the left of the :=
.
Note how, unlike the previous binding example, this one ends up with two storage locations (associated with the symbols %H
and %G
) being updated, corresponding to the two hashes in the List
returned by the GetHashes
call.
The effect of the list assignment in your original code
With your code as it was, you were copying all of the data/values from the right hand side of the =
, item by item (so a total of 10 copying acts), into the hash bound to the first variable (%H
) on the left.
This is due to the precise semantics of assignment (use of =
) which are determined by the individual targets on the left of the =
, starting with the first, until it's had its fill, then the second, and so on.
If an assignment target is an Associative
(eg %H
), it greedily slurps everything it can from the right of the =
. This is called "list assignment".
So what happens with your original code is that %H
slurps up the first five elements in the first hash returned by GetHashes
, then keeps going, so the five elements from the second hash returned by the sub are also assigned to %H
.
And because the keys of this second batch of pairs are the same as the keys of the first, this second batch overwrites the first five.
So %H
ends up with just the second batch of pairs, and %G
stays empty, the result you see.
A similar deal applies for Positional
assignment targets, eg a variable named @array
.
The only assignment targets in standard Raku that are not greedy in this fashion are Scalar
targets such as $bar
. So you can usefully write, say, my ($bar, @bam) = 1, 2, 3;
and end up with $bar
assigned 1
, and @bam
ending up as [2 3]
. Hence @p6steve's answer.
Footnotes
[1] If =
is used in some code, but assignment to containers on the left isn't what that expression means, Raku does what the writer presumably means instead. For example:
constant list = 1, 2, 3;
does not temporarily assign the List
(1, 2, 3)
to the symbol list
at run-time but instead permanently binds it at compile-time. In other words, it does the same as constant list := 1, 2, 3;
. (Most folk just write constant list = 1, 2, 3;
.)
my int @list = 1, 2, 3;
does not add any Scalar
containers to the native array @list
but instead directly writes native integers.