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