user
Task

Define two associative arrays, where one represents the following "base" data:

:   :   :   :   :   |  |  |
                    | --- | --- |
                    | **Key** | **Value** |
                    | "name" | "Rocket Skates" |
                    | "price" | 12.75 |
                    | "color" | "yellow" |

And the other represents "update" data:

:   :   :   :   :   |  |  |
                    | --- | --- |
                    | **Key** | **Value** |
                    | "price" | 15.25 |
                    | "color" | "red" |
                    | "year" | 1974 |

Merge these into a new associative array that contains every key found in either of the source ones. Each key should map to the value in the second (update) table if that exists, or else to the value in the first (base) table. If possible, do this in a way that does not mutate the original two associative arrays. Obviously this should be done in a way that would work for any data, not just the specific data given here, but in this example the result should be:

:   :   :   :   :   |  |  |
                    | --- | --- |
                    | **Key** | **Value** |
                    | "name" | "Rocket Skates" |
                    | "price" | 15.25 |
                    | "color" | "red" |
                    | "year" | 1974 |
assistant
(formerly Perl 6)

**Works with**: Rakudo version 2019.11

I must say I somewhat disagree with the terminology. The requested operation is an update not a merge. Demonstrate both an update and a merge. Associative arrays are commonly called hashes in Raku.

```
# Show original hashes
say my %base   = :name('Rocket Skates'), :price<12.75>, :color<yellow>;
say my %update = :price<15.25>, :color<red>, :year<1974>;

# Need to assign to anonymous hash to get the desired results and avoid mutating
# TIMTOWTDI
say "\nUpdate:\n", join "\n", sort %=%base, %update;
# Same
say "\nUpdate:\n", {%base, %update}.sort.join: "\n";

say "\nMerge:\n", join "\n", sort ((%=%base).push: %update)».join: ', ';
# Same
say "\nMerge:\n", ({%base}.push: %update)».join(', ').sort.join: "\n";

# Demonstrate unmutated hashes
say "\n", %base, "\n", %update;

```

Output:

```
{color => yellow, name => Rocket Skates, price => 12.75}
{color => red, price => 15.25, year => 1974}

Update:
color	red
name	Rocket Skates
price	15.25
year	1974

Update:
color	red
name	Rocket Skates
price	15.25
year	1974

Merge:
color	yellow, red
name	Rocket Skates
price	12.75, 15.25
year	1974

Merge:
color	yellow, red
name	Rocket Skates
price	12.75, 15.25
year	1974

{color => yellow, name => Rocket Skates, price => 12.75}
{color => red, price => 15.25, year => 1974}
```