Associative array/Merging: Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
(Realize in F#)
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 104:
color: red
</pre>
 
=={{header|AWK}}==
<lang AWK>
Line 233 ⟶ 234:
</pre>
// Minimum positive multiple in base 10 using only 0 and 1. Nigel Galloway: March 9th., 2020
 
=={{header|Factor}}==
The <code>assoc-union</code> word does this. <code>assoc-union!</code> is a variant that mutates the first associative array.
Line 483 ⟶ 485:
price 12.75, 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 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;</lang>
{{out}}
<pre>{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}</pre>
 
=={{header|Phix}}==
Line 604 ⟶ 555:
{{output}}
<pre>{'name': 'Rocket Skates', 'price': 15.25, 'color': 'red', 'year': 1974}</pre>
 
=={{header|Raku}}==
(formerly 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 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;</lang>
{{out}}
<pre>{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}</pre>
 
=={{header|REXX}}==
Line 700 ⟶ 703:
"price": "15.25",
}</pre>
 
 
=={{header|Swift}}==
10,343

edits