Associative array/Merging: Difference between revisions

Content added Content deleted
m (→‎{{header|zkl}}: I like this way better)
(→‎{{header|Perl 6}}: Add a Perl 6 example)
Line 155: Line 155:
{{output}}
{{output}}
<pre>{"color": "red", "name": "Rocket Skates", "price": 15.25, "year": 1974}</pre>
<pre>{"color": "red", "name": "Rocket Skates", "price": 15.25, "year": 1974}</pre>

=={{header|Perl 6}}==
{{works with|Rakudo|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 Perl 6.

<lang perl6># 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 (or a named hash) to get the desired results and avoid mutating
say "\nUpdate:\n", join "\n", %=%base, %update;

say "\nMerge:\n", join "\n", ((%=%base).push: %update)».join: ', ';

# Demonstrate unmutated hashes
say "\n", %base, "\n", %update;</lang>
{{out}}
<pre>{color => yellow, name => Rocket Skates, price => 12.75}
{color => red, price => 15.25, year => 1974}

Update:
price 15.25
name Rocket Skates
year 1974
color red

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

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


=={{header|Python}}==
=={{header|Python}}==