Associative array/Merging: Difference between revisions

From Rosetta Code
Content added Content deleted
m (→‎{{header|C sharp}}: Regularize header markup to recommended on category page)
m (Moved Vlang entry into correct alphabetical order.)
 
(13 intermediate revisions by 10 users not shown)
Line 47: Line 47:
{{trans|Python}}
{{trans|Python}}


<lang 11l>V base = [‘name’ = ‘Rocket Skates’, ‘price’ = ‘12.75’, ‘color’ = ‘yellow’]
<syntaxhighlight lang="11l">V base = [‘name’ = ‘Rocket Skates’, ‘price’ = ‘12.75’, ‘color’ = ‘yellow’]
V update = [‘price’ = ‘15.25’, ‘color’ = ‘red’, ‘year’ = ‘1974’]
V update = [‘price’ = ‘15.25’, ‘color’ = ‘red’, ‘year’ = ‘1974’]


Line 53: Line 53:
result.update(update)
result.update(update)


print(result)</lang>
print(result)</syntaxhighlight>


{{out}}
{{out}}
Line 61: Line 61:


=={{header|Ada}}==
=={{header|Ada}}==
<lang Ada>with Ada.Text_Io;
<syntaxhighlight lang="ada">with Ada.Text_Io;
with Ada.Containers.Indefinite_Ordered_Maps;
with Ada.Containers.Indefinite_Ordered_Maps;


Line 129: Line 129:
New_Line;
New_Line;


end Merge_Maps;</lang>
end Merge_Maps;</syntaxhighlight>
{{out}}
{{out}}
<pre>Original:
<pre>Original:
Line 150: Line 150:
{{works with|ALGOL 68G|Any - tested with release 2.8.3.win32}}
{{works with|ALGOL 68G|Any - tested with release 2.8.3.win32}}
Uses the associative array implementations in [[ALGOL_68/prelude]].
Uses the associative array implementations in [[ALGOL_68/prelude]].
<lang algol68># associative array merging #
<syntaxhighlight lang="algol68"># associative array merging #
# the modes allowed as associative array element values - change to suit #
# the modes allowed as associative array element values - change to suit #
Line 201: Line 201:
);
);
e := NEXT c
e := NEXT c
OD</lang>
OD</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 214: Line 214:
The method with AppleScript "records" is to concatenate them. The result is a third record containing the labels from both contributors, the values from the record to the left of the operator being kept where labels are shared. The bars with some of the labels below are to distinguish them as "user" labels from tokenised ones that are provided as standards for use by scriptable applications and scripting additions. However, merging records works with tokenised labels too.
The method with AppleScript "records" is to concatenate them. The result is a third record containing the labels from both contributors, the values from the record to the left of the operator being kept where labels are shared. The bars with some of the labels below are to distinguish them as "user" labels from tokenised ones that are provided as standards for use by scriptable applications and scripting additions. However, merging records works with tokenised labels too.


<lang applescript>set baseRecord to {|name|:"Rocket Skates", price:12.75, |color|:"yellow"}
<syntaxhighlight lang="applescript">set baseRecord to {|name|:"Rocket Skates", price:12.75, |color|:"yellow"}
set updateRecord to {price:15.25, |color|:"red", |year|:1974}
set updateRecord to {price:15.25, |color|:"red", |year|:1974}


set mergedRecord to updateRecord & baseRecord
set mergedRecord to updateRecord & baseRecord
return mergedRecord</lang>
return mergedRecord</syntaxhighlight>


{{output}}
{{output}}
<lang applescript>{price:15.25, |color|:"red", |year|:1974, |name|:"Rocket Skates"}</lang>
<syntaxhighlight lang="applescript">{price:15.25, |color|:"red", |year|:1974, |name|:"Rocket Skates"}</syntaxhighlight>


=={{header|Arturo}}==
=={{header|Arturo}}==


<lang rebol>details: #[name: "Rocket Skates" price: 12.75 colour: 'yellow]
<syntaxhighlight lang="rebol">details: #[name: "Rocket Skates" price: 12.75 colour: 'yellow]
newDetails: extend details #[price: 15.25 colour: 'red year: 1974]
newDetails: extend details #[price: 15.25 colour: 'red year: 1974]


print newDetails</lang>
print newDetails</syntaxhighlight>


{{out}}
{{out}}
Line 235: Line 235:


=={{header|AutoHotkey}}==
=={{header|AutoHotkey}}==
<lang AutoHotkey>merge(base, update){
<syntaxhighlight lang="autohotkey">merge(base, update){
Merged := {}
Merged := {}
for k, v in base
for k, v in base
Line 242: Line 242:
Merged[k] := v
Merged[k] := v
return Merged
return Merged
}</lang>
}</syntaxhighlight>
Examples:<lang AutoHotkey>base := {"name":"Rocket Skates", "price":12.75, "color":"yellow"}
Examples:<syntaxhighlight lang="autohotkey">base := {"name":"Rocket Skates", "price":12.75, "color":"yellow"}
update := {"price":15.25, "color":"red", "year":1974}
update := {"price":15.25, "color":"red", "year":1974}
Merged := merge(base, update)
Merged := merge(base, update)
for k, v in Merged
for k, v in Merged
result .= k " : " v "`n"
result .= k " : " v "`n"
MsgBox % result</lang>
MsgBox % result</syntaxhighlight>
Outputs:<pre>color : red
Outputs:<pre>color : red
name : Rocket Skates
name : Rocket Skates
Line 255: Line 255:


=={{header|AWK}}==
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f ASSOCIATIVE_ARRAY_MERGING.AWK
# syntax: GAWK -f ASSOCIATIVE_ARRAY_MERGING.AWK
#
#
Line 283: Line 283:
}
}
}
}
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 304: Line 304:


=={{header|B4X}}==
=={{header|B4X}}==
<lang b4x>Dim m1 As Map = CreateMap("name": "Rocket Skates", "price": 12.75, "color": "yellow")
<syntaxhighlight lang="b4x">Dim m1 As Map = CreateMap("name": "Rocket Skates", "price": 12.75, "color": "yellow")
Dim m2 As Map = CreateMap("price": 15.25, "color": "red", "year": 1974)
Dim m2 As Map = CreateMap("price": 15.25, "color": "red", "year": 1974)
Dim merged As Map
Dim merged As Map
Line 313: Line 313:
Next
Next
Next
Next
Log(merged)</lang>
Log(merged)</syntaxhighlight>


=={{header|BaCon}}==
=={{header|BASIC}}==
==={{header|BaCon}}===
<lang bacon>DECLARE base$, update$, merge$ ASSOC STRING
<syntaxhighlight lang="bacon">DECLARE base$, update$, merge$ ASSOC STRING


base$("name") = "Rocket Skates"
base$("name") = "Rocket Skates"
Line 342: Line 343:
FOR x$ IN OBTAIN$(merge$)
FOR x$ IN OBTAIN$(merge$)
PRINT x$, " : ", merge$(x$)
PRINT x$, " : ", merge$(x$)
NEXT</lang>
NEXT</syntaxhighlight>
{{out}}
{{out}}
<pre>Base array
<pre>Base array
Line 361: Line 362:


=={{header|C++}}==
=={{header|C++}}==
<lang cpp>#include <iostream>
<syntaxhighlight lang="cpp">#include <iostream>
#include <string>
#include <string>
#include <map>
#include <map>
Line 388: Line 389:
std::cout << "key: " << i.first << ", value: " << i.second << '\n';
std::cout << "key: " << i.first << ", value: " << i.second << '\n';
return 0;
return 0;
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 399: Line 400:


=={{header|C sharp|C#}}==
=={{header|C sharp|C#}}==
<lang csharp>using System;
<syntaxhighlight lang="csharp">using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq;
using System.Linq;
Line 424: Line 425:
}
}
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 433: Line 434:


=={{header|Clojure}}==
=={{header|Clojure}}==
<syntaxhighlight lang="clojure">
<lang Clojure>
(defn update-map [base update]
(defn update-map [base update]
(merge base update))
(merge base update))
Line 443: Line 444:
"color" "red"
"color" "red"
"year" "1974"})
"year" "1974"})
</syntaxhighlight>
</lang>


{{out}}
{{out}}
Line 451: Line 452:


=={{header|Crystal}}==
=={{header|Crystal}}==
<lang crystal>base = {"name" => "Rocket Skates", "price" => 12.75, "color" => "yellow"}
<syntaxhighlight lang="crystal">base = {"name" => "Rocket Skates", "price" => 12.75, "color" => "yellow"}
update = { "price" => 15.25, "color" => "red", "year" => 1974 }
update = { "price" => 15.25, "color" => "red", "year" => 1974 }


puts base.merge(update)</lang>
puts base.merge(update)</syntaxhighlight>


{{out}}
{{out}}
Line 462: Line 463:
=={{header|Common Lisp}}==
=={{header|Common Lisp}}==
In Common Lisp, the value of a key in an alist or a plist is defined as the first value in the list with a matching key or indicator. Thus, all that is necessary to implement this algorithm for either is the following:
In Common Lisp, the value of a key in an alist or a plist is defined as the first value in the list with a matching key or indicator. Thus, all that is necessary to implement this algorithm for either is the following:
<lang lisp>
<syntaxhighlight lang="lisp">
(append list2 list1)
(append list2 list1)
</syntaxhighlight>
</lang>


These implementations for alists and plists are more complicated, but avoid duplicate keys in the results:
These implementations for alists and plists are more complicated, but avoid duplicate keys in the results:
<lang lisp>
<syntaxhighlight lang="lisp">
(defun merge-alists (alist1 alist2)
(defun merge-alists (alist1 alist2)
(nconc
(nconc
Line 483: Line 484:
:do (setf (getf res key) val))
:do (setf (getf res key) val))
res))
res))
</syntaxhighlight>
</lang>


=={{header|Dart}}==
=={{header|Dart}}==
<lang javascript>
<syntaxhighlight lang="javascript">
main() {
main() {
var base = {
var base = {
Line 508: Line 509:


}
}
</syntaxhighlight>
</lang>
=={{header|Delphi}}==
=={{header|Delphi}}==
{{libheader| System.Generics.Collections}}
{{libheader| System.Generics.Collections}}
{{Trans|C#}}
{{Trans|C#}}
<syntaxhighlight lang="delphi">
<lang Delphi>
program Associative_arrayMerging;
program Associative_arrayMerging;


Line 555: Line 556:
end.
end.


</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 565: Line 566:
=={{header|Elixir}}==
=={{header|Elixir}}==
Elixir has a built-in hashmap type, called [https://hexdocs.pm/elixir/Map.html Map].
Elixir has a built-in hashmap type, called [https://hexdocs.pm/elixir/Map.html Map].
<lang Elixir>base = %{name: "Rocket Skates", price: 12.75, color: "yellow"}
<syntaxhighlight lang="elixir">base = %{name: "Rocket Skates", price: 12.75, color: "yellow"}
update = %{price: 15.25, color: "red", year: 1974}
update = %{price: 15.25, color: "red", year: 1974}
result = Map.merge(base, update)
result = Map.merge(base, update)
IO.inspect(result)</lang>
IO.inspect(result)</syntaxhighlight>
{{out}}
{{out}}
<pre>%{color: "red", name: "Rocket Skates", price: 15.25, year: 1974}</pre>
<pre>%{color: "red", name: "Rocket Skates", price: 15.25, year: 1974}</pre>
The above sample uses [https://hexdocs.pm/elixir/Atom.html atoms] as the key type. If strings are needed, the <code>base</code> map would look like this:
The above sample uses [https://hexdocs.pm/elixir/Atom.html atoms] as the key type. If strings are needed, the <code>base</code> map would look like this:
<lang Elixir>base = %{"name" => "Rocket Skates", "price" => 12.75, "color" => "yellow"}</lang>
<syntaxhighlight lang="elixir">base = %{"name" => "Rocket Skates", "price" => 12.75, "color" => "yellow"}</syntaxhighlight>


=={{header|F_Sharp|F#}}==
=={{header|F_Sharp|F#}}==
<lang fsharp>
<syntaxhighlight lang="fsharp">
type N = |Price of float|Name of string|Year of int|Colour of string
type N = |Price of float|Name of string|Year of int|Colour of string
let n=Map<string,N>[("name",Name("Rocket Skates"));("price",Price(12.75));("colour",Colour("yellow"))]
let n=Map<string,N>[("name",Name("Rocket Skates"));("price",Price(12.75));("colour",Colour("yellow"))]
Line 581: Line 582:
let ng=(Map.toList n)@(Map.toList g)|>Map.ofList
let ng=(Map.toList n)@(Map.toList g)|>Map.ofList
printfn "%A" ng
printfn "%A" ng
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 593: Line 594:
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.
{{works with|Factor|0.99 2019-10-06}}
{{works with|Factor|0.99 2019-10-06}}
<lang factor>USING: assocs prettyprint ;
<syntaxhighlight lang="factor">USING: assocs prettyprint ;


{ { "name" "Rocket Skates" } { "price" 12.75 } { "color" "yellow" } }
{ { "name" "Rocket Skates" } { "price" 12.75 } { "color" "yellow" } }
{ { "price" 15.25 } { "color" "red" } { "year" 1974 } }
{ { "price" 15.25 } { "color" "red" } { "year" 1974 } }
assoc-union .</lang>
assoc-union .</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 607: Line 608:
}
}
</pre>
</pre>

=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
void local fn DoIt
CFDictionaryRef base = @{@"name" :@"Rocket Skates", @"price":@12.75, @"color":@"yellow"}
CFDictionaryRef update = @{@"price":@15.25, @"color":@"red", @"year":@1974}
CFMutableDictionaryRef merged = fn MutableDictionaryWithDictionary( base )
MutableDictionaryAddEntriesFromDictionary( merged, update )
print merged
end fn

fn DoIt

HandleEvents
</syntaxhighlight>



=={{header|Go}}==
=={{header|Go}}==
<lang go>package main
<syntaxhighlight lang="go">package main


import "fmt"
import "fmt"
Line 631: Line 650:
result := merge(base, update)
result := merge(base, update)
fmt.Println(result)
fmt.Println(result)
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 641: Line 660:
There are various approaches, all of which would be strictly typed.<br>
There are various approaches, all of which would be strictly typed.<br>
For example, if we want to treat the input and output records as all sharing the same type:
For example, if we want to treat the input and output records as all sharing the same type:
<lang haskell>data Item = Item
<syntaxhighlight lang="haskell">data Item = Item
{ name :: Maybe String
{ name :: Maybe String
, price :: Maybe Float
, price :: Maybe Float
Line 657: Line 676:
itemFromMerge
itemFromMerge
(Item (Just "Rocket Skates") (Just 12.75) (Just "yellow") Nothing)
(Item (Just "Rocket Skates") (Just 12.75) (Just "yellow") Nothing)
(Item Nothing (Just 15.25) (Just "red") (Just 1974))</lang>
(Item Nothing (Just 15.25) (Just "red") (Just 1974))</syntaxhighlight>
{{Out}}
{{Out}}
<pre>Item {name = Just "Rocket Skates", price = Just 15.25, color = Just "red", year = Just 1974}</pre>
<pre>Item {name = Just "Rocket Skates", price = Just 15.25, color = Just "red", year = Just 1974}</pre>


=={{header|Icon}} and {{header|Unicon}}==
=={{header|Icon}} and {{header|Unicon}}==
<lang unicon>procedure main()
<syntaxhighlight lang="unicon">procedure main()
local base, update, master, f, k
local base, update, master, f, k


Line 683: Line 702:
write(k, " = ", master[k])
write(k, " = ", master[k])
}
}
end</lang>
end</syntaxhighlight>


{{out}}
{{out}}
Line 693: Line 712:


=={{header|J}}==
=={{header|J}}==
<syntaxhighlight lang="j">
<lang J>
merge=: ,. NB. use: update merge original
merge=: ,. NB. use: update merge original
compress=: #"1~ ~:@:keys
compress=: #"1~ ~:@:keys
Line 700: Line 719:
get=: [: > ((i.~ keys)~ <)~ { values@:] NB. key get (associative array)
get=: [: > ((i.~ keys)~ <)~ { values@:] NB. key get (associative array)
pair=: [: |: <;._2;._2
pair=: [: |: <;._2;._2
</syntaxhighlight>
</lang>
Exercise the definitions. Interactive J prompts with 3 space indentation.
Exercise the definitions. Interactive J prompts with 3 space indentation.
<pre>
<pre>
Line 752: Line 771:


=={{header|Java}}==
=={{header|Java}}==
<p>
<lang java>import java.util.*;
See also, [https://rosettacode.org/wiki/Associative_array/Creation#Java Java - Associative array/Creation].
</p>
<p>
This task illustrates the difference between statically typed languages, and dynamically typed.<br />
With exception to the <code>var</code> keyword, Java is statically typed.
So, using an undefined, non-static, <kbd>value</kbd>, creates a trivial situation.
</p>
<p>
A good way to understand this task is that it's not possible in a static-typed language.<br />
It defeats the purpose of defining the data-type.<br />
For Java, if you're going to use <code>Object</code> as your data-type, you need to re-evaluate the method of abstraction.
</p>
<p>
Java offers <kbd>generics</kbd>, <kbd>interfaces</kbd>, and <kbd>abstract classes</kbd> for this task.
</p>
<p>
Considering this, to complete this specific task, I would just store the values as <kbd>strings</kbd>.
</p>
<syntaxhighlight lang="java">
import java.util.LinkedHashMap;
import java.util.Map;
</syntaxhighlight>
<syntaxhighlight lang="java">
Map<String, String> mapA = new LinkedHashMap<>();
mapA.put("name", "Rocket Skates");
mapA.put("price", "12.75");
mapA.put("color", "yellow");


Map<String, String> mapB = new LinkedHashMap<>();
class MergeMaps {
mapB.put("price", "15.25");
public static void main(String[] args) {
mapB.put("color", "red");
Map<String, Object> base = new HashMap<>();
base.put("name", "Rocket Skates");
mapB.put("year", "1974");
base.put("price", 12.75);
base.put("color", "yellow");
Map<String, Object> update = new HashMap<>();
update.put("price", 15.25);
update.put("color", "red");
update.put("year", 1974);


Map<String, Object> result = new HashMap<>(base);
Map<String, String> mapC = new LinkedHashMap<>();
result.putAll(update);
mapC.putAll(mapA);
mapC.putAll(mapB);
</syntaxhighlight>
To show that the original <kbd>map</kbd>s are not affected.
<syntaxhighlight lang="java">
for(Map.Entry<String, String> entry : mapA.entrySet())
System.out.printf("%-20s%s%n", entry.getKey(), entry.getValue());


for(Map.Entry<String, String> entry : mapB.entrySet())
System.out.println(result);
System.out.printf("%-20s%s%n", entry.getKey(), entry.getValue());
}

}</lang>
for(Map.Entry<String, String> entry : mapC.entrySet())
{{output}}
System.out.printf("%-20s%s%n", entry.getKey(), entry.getValue());
<pre>{name=Rocket Skates, color=red, year=1974, price=15.25}</pre>
</syntaxhighlight>
<pre>
name Rocket Skates
price 12.75
color yellow
</pre>
<pre>
price 15.25
color red
year 1974
</pre>
<pre>
name Rocket Skates
price 15.25
color red
year 1974
</pre>
<p>
While not recommended, due to scalability, if you did want to use an <code>Object</code> as the value, you could use the following implementation.<br />
This will produce the same output as above.
</p>
<syntaxhighlight lang="java">
Map<String, Object> mapA = new LinkedHashMap<>();
mapA.put("name", "Rocket Skates");
mapA.put("price", 12.75);
mapA.put("color", "yellow");

Map<String, Object> mapB = new LinkedHashMap<>();
mapB.put("price", 15.25);
mapB.put("color", "red");
mapB.put("year", 1974);

Map<String, Object> mapC = new LinkedHashMap<>();
mapC.putAll(mapA);
mapC.putAll(mapB);
</syntaxhighlight>


=={{header|JavaScript}}==
=={{header|JavaScript}}==
<lang javascript>(() => {
<syntaxhighlight lang="javascript">(() => {
'use strict';
'use strict';


Line 792: Line 874:
null, 2
null, 2
))
))
})();</lang>
})();</syntaxhighlight>
{{Out}}
{{Out}}
<pre>{
<pre>{
Line 845: Line 927:


=={{header|Kotlin}}==
=={{header|Kotlin}}==
<lang scala>
<syntaxhighlight lang="scala">
fun main() {
fun main() {
val base = HashMap<String,String>()
val base = HashMap<String,String>()
Line 865: Line 947:
println("merged: $merged")
println("merged: $merged")
}
}
</syntaxhighlight>
</lang>
{{Output}}
{{Output}}
<pre>
<pre>
Line 874: Line 956:


=={{header|Lua}}==
=={{header|Lua}}==
<lang Lua>base = {name="Rocket Skates", price=12.75, color="yellow"}
<syntaxhighlight lang="lua">base = {name="Rocket Skates", price=12.75, color="yellow"}
update = {price=15.25, color="red", year=1974}
update = {price=15.25, color="red", year=1974}


Line 891: Line 973:
for key,val in pairs(result) do
for key,val in pairs(result) do
print(string.format("%s: %s", key, val))
print(string.format("%s: %s", key, val))
end</lang>
end</syntaxhighlight>


{{output}}
{{output}}
Line 900: Line 982:


=={{header|Mathematica}} / {{header|Wolfram Language}}==
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<lang Mathematica>a1 = <|"name" -> "Rocket Skates", "price" -> 12.75, "color" -> "yellow"|>;
<syntaxhighlight lang="mathematica">a1 = <|"name" -> "Rocket Skates", "price" -> 12.75, "color" -> "yellow"|>;
a2 = <|"price" -> 15.25, "color" -> "red", "year" -> 1974|>;
a2 = <|"price" -> 15.25, "color" -> "red", "year" -> 1974|>;
Merge[{a1, a2}, Last]</lang>
Merge[{a1, a2}, Last]</syntaxhighlight>
{{out}}
{{out}}
<pre><|"name" -> "Rocket Skates", "price" -> 15.25, "color" -> "red", "year" -> 1974|></pre>
<pre><|"name" -> "Rocket Skates", "price" -> 15.25, "color" -> "red", "year" -> 1974|></pre>
Line 908: Line 990:
=={{header|MiniScript}}==
=={{header|MiniScript}}==
MiniScript supports merging maps with the + operator.
MiniScript supports merging maps with the + operator.
<lang MiniScript>base = {"name":"Rocket Skates", "price":12.75, "color":"yellow"}
<syntaxhighlight lang="miniscript">base = {"name":"Rocket Skates", "price":12.75, "color":"yellow"}
update = {"price":15.25, "color":"red", "year":1974}
update = {"price":15.25, "color":"red", "year":1974}


result = base + update
result = base + update


print result</lang>
print result</syntaxhighlight>


{{output}}
{{output}}
Line 919: Line 1,001:


=={{header|Nim}}==
=={{header|Nim}}==
<lang Nim>import tables
<syntaxhighlight lang="nim">import tables


let t1 = {"name": "Rocket Skates", "price": "12.75", "color": "yellow"}.toTable
let t1 = {"name": "Rocket Skates", "price": "12.75", "color": "yellow"}.toTable
Line 928: Line 1,010:
t3[key] = value
t3[key] = value


echo t3</lang>
echo t3</syntaxhighlight>


{{out}}
{{out}}
Line 935: Line 1,017:
=={{header|Objective-C}}==
=={{header|Objective-C}}==
{{works with|Cocoa}}
{{works with|Cocoa}}
<lang objc>#import <Foundation/Foundation.h>
<syntaxhighlight lang="objc">#import <Foundation/Foundation.h>


int main(void) {
int main(void) {
Line 948: Line 1,030:
}
}
return 0;
return 0;
}</lang>
}</syntaxhighlight>
{{output}}
{{output}}
<pre>{
<pre>{
Line 964: Line 1,046:


Helper code for all 3 versions:
Helper code for all 3 versions:
<syntaxhighlight lang="ocaml">
<lang OCaml>
type ty =
type ty =
| TFloat of float
| TFloat of float
Line 981: Line 1,063:
Printf.printf "%s: %s\n" key (string_of_ty el)
Printf.printf "%s: %s\n" key (string_of_ty el)
;;
;;
</syntaxhighlight>
</lang>


Association list : naive and functional approach.
Association list : naive and functional approach.
<syntaxhighlight lang="ocaml">
<lang OCaml>
let l1 : assoc list = [
let l1 : assoc list = [
("name", TString "Rocket Skates");
("name", TString "Rocket Skates");
Line 1,007: Line 1,089:


let l' = merge_assoc_list l1 l2 ;;
let l' = merge_assoc_list l1 l2 ;;
</syntaxhighlight>
</lang>


Binary tree/Map functor : proper functional approach.
Binary tree/Map functor : proper functional approach.
{{works with|OCaml|above 4.03 for union function}}
{{works with|OCaml|above 4.03 for union function}}
<syntaxhighlight lang="ocaml">
<lang OCaml>
module StringMap = Map.Make(String) ;;
module StringMap = Map.Make(String) ;;


Line 1,037: Line 1,119:


print_map m' ;;
print_map m' ;;
</syntaxhighlight>
</lang>


Hash table : imperative/mutable approach.
Hash table : imperative/mutable approach.
<syntaxhighlight lang="ocaml">
<lang OCaml>
(* Updates the base table with the bindings from add *)
(* Updates the base table with the bindings from add *)
let hash_merge (base : (string, ty) Hashtbl.t) (add : (string, ty) Hashtbl.t) : unit =
let hash_merge (base : (string, ty) Hashtbl.t) (add : (string, ty) Hashtbl.t) : unit =
Line 1,061: Line 1,143:


print_hashtbl h1 ;;
print_hashtbl h1 ;;
</syntaxhighlight>
</lang>


=={{header|Ol}}==
=={{header|Ol}}==
Since version 3.1.1 function `ff-union` changed from `(ff-union a1 a2 collide)` to `(ff-union collide a1 a2 ...)`!
<lang scheme>

<syntaxhighlight lang="scheme">
(define a1 {
(define a1 {
'name "Rocket Skates"
'name "Rocket Skates"
Line 1,081: Line 1,165:


(define (collide a b) b) ; will use new key value
(define (collide a b) b) ; will use new key value
(print "merged a1 a2: " (ff-union a1 a2 collide))
(print "merged a1 a2: " (ff-union collide a1 a2))
</syntaxhighlight>
</lang>
{{Out}}
{{Out}}
<pre>
<pre>
Line 1,091: Line 1,175:


=={{header|Perl}}==
=={{header|Perl}}==
<lang perl>use strict;
<syntaxhighlight lang="perl">use strict;
use warnings;
use warnings;


Line 1,105: Line 1,189:
$merge{$_} = [$base{$_}] for keys %base;
$merge{$_} = [$base{$_}] for keys %base;
push @{$merge{$_}}, $more{$_} for keys %more;
push @{$merge{$_}}, $more{$_} for keys %more;
printf "%-7s %s\n", $_, join ', ', @{$merge{$_}} for sort keys %merge;</lang>
printf "%-7s %s\n", $_, join ', ', @{$merge{$_}} for sort keys %merge;</syntaxhighlight>
{{output}}
{{output}}
<pre>Update
<pre>Update
Line 1,120: Line 1,204:


=={{header|Phix}}==
=={{header|Phix}}==
<!--<lang Phix>(phixonline)-->
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">d1</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">new_dict</span><span style="color: #0000FF;">({{</span><span style="color: #008000;">"name"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Rocket Skates"</span><span style="color: #0000FF;">},{</span><span style="color: #008000;">"price"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">12.75</span><span style="color: #0000FF;">},{</span><span style="color: #008000;">"color"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"yellow"</span><span style="color: #0000FF;">}}),</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">d1</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">new_dict</span><span style="color: #0000FF;">({{</span><span style="color: #008000;">"name"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Rocket Skates"</span><span style="color: #0000FF;">},{</span><span style="color: #008000;">"price"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">12.75</span><span style="color: #0000FF;">},{</span><span style="color: #008000;">"color"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"yellow"</span><span style="color: #0000FF;">}}),</span>
Line 1,129: Line 1,213:
<span style="color: #008080;">include</span> <span style="color: #000000;">builtins</span><span style="color: #0000FF;">/</span><span style="color: #000000;">map</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
<span style="color: #008080;">include</span> <span style="color: #000000;">builtins</span><span style="color: #0000FF;">/</span><span style="color: #000000;">map</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
<span style="color: #0000FF;">?</span><span style="color: #000000;">pairs</span><span style="color: #0000FF;">(</span><span style="color: #000000;">d3</span><span style="color: #0000FF;">)</span>
<span style="color: #0000FF;">?</span><span style="color: #000000;">pairs</span><span style="color: #0000FF;">(</span><span style="color: #000000;">d3</span><span style="color: #0000FF;">)</span>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
<pre>
<pre>
Line 1,136: Line 1,220:


=={{header|Phixmonti}}==
=={{header|Phixmonti}}==
<lang Phixmonti>include ..\Utilitys.pmt
<syntaxhighlight lang="phixmonti">include ..\Utilitys.pmt


def scand /# dict key -- dict n #/
def scand /# dict key -- dict n #/
Line 1,182: Line 1,266:


pstack
pstack
</syntaxhighlight>
</lang>


=={{header|PHP}}==
=={{header|PHP}}==
<lang php><?
<syntaxhighlight lang="php"><?
$base = array("name" => "Rocket Skates", "price" => 12.75, "color" => "yellow");
$base = array("name" => "Rocket Skates", "price" => 12.75, "color" => "yellow");
$update = array("price" => 15.25, "color" => "red", "year" => 1974);
$update = array("price" => 15.25, "color" => "red", "year" => 1974);
Line 1,191: Line 1,275:
$result = $update + $base; // Notice that the order is reversed
$result = $update + $base; // Notice that the order is reversed
print_r($result);
print_r($result);
?></lang>
?></syntaxhighlight>
{{output}}
{{output}}
<pre>Array
<pre>Array
Line 1,202: Line 1,286:


Alternative:
Alternative:
<lang php><?
<syntaxhighlight lang="php"><?
$base = array("name" => "Rocket Skates", "price" => 12.75, "color" => "yellow");
$base = array("name" => "Rocket Skates", "price" => 12.75, "color" => "yellow");
$update = array("price" => 15.25, "color" => "red", "year" => 1974);
$update = array("price" => 15.25, "color" => "red", "year" => 1974);
Line 1,208: Line 1,292:
$result = array_merge($base, $update);
$result = array_merge($base, $update);
print_r($result);
print_r($result);
?></lang>
?></syntaxhighlight>
{{output}}
{{output}}
<pre>Array
<pre>Array
Line 1,219: Line 1,303:


=={{header|PureBasic}}==
=={{header|PureBasic}}==
<lang PureBasic>NewMap m1.s()
<syntaxhighlight lang="purebasic">NewMap m1.s()
NewMap m2.s()
NewMap m2.s()
NewMap m3.s()
NewMap m3.s()
Line 1,239: Line 1,323:
ForEach m3()
ForEach m3()
Debug MapKey(m3())+" : "+m3()
Debug MapKey(m3())+" : "+m3()
Next</lang>
Next</syntaxhighlight>
{{out}}
{{out}}
<pre>price : 15.25
<pre>price : 15.25
Line 1,249: Line 1,333:
As of Python 3.5, this can be solved with the dictionary unpacking operator.
As of Python 3.5, this can be solved with the dictionary unpacking operator.
{{works with|Python|3.5+}}
{{works with|Python|3.5+}}
<lang Python>base = {"name":"Rocket Skates", "price":12.75, "color":"yellow"}
<syntaxhighlight lang="python">base = {"name":"Rocket Skates", "price":12.75, "color":"yellow"}
update = {"price":15.25, "color":"red", "year":1974}
update = {"price":15.25, "color":"red", "year":1974}


result = {**base, **update}
result = {**base, **update}


print(result)</lang>
print(result)</syntaxhighlight>
{{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>


;Alternative:
;Alternative:
<lang Python>base = {"name":"Rocket Skates", "price":12.75, "color":"yellow"}
<syntaxhighlight lang="python">base = {"name":"Rocket Skates", "price":12.75, "color":"yellow"}
update = {"price":15.25, "color":"red", "year":1974}
update = {"price":15.25, "color":"red", "year":1974}


Line 1,265: Line 1,349:
result.update(update)
result.update(update)


print(result)</lang>
print(result)</syntaxhighlight>
{{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>
Line 1,271: Line 1,355:
;New alternative using '|'
;New alternative using '|'


<lang python>Python 3.9.0 (tags/v3.9.0:9cf6752, Oct 5 2020, 15:34:40) [MSC v.1927 64 bit (AMD64)] on win32
<syntaxhighlight lang="python">Python 3.9.0 (tags/v3.9.0:9cf6752, Oct 5 2020, 15:34:40) [MSC v.1927 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
Type "help", "copyright", "credits" or "license()" for more information.
>>> base = {"name":"Rocket Skates", "price":12.75, "color":"yellow"}
>>> base = {"name":"Rocket Skates", "price":12.75, "color":"yellow"}
Line 1,278: Line 1,362:
>>> result
>>> result
{'name': 'Rocket Skates', 'price': 15.25, 'color': 'red', 'year': 1974}
{'name': 'Rocket Skates', 'price': 15.25, 'color': 'red', 'year': 1974}
>>> </lang>
>>> </syntaxhighlight>


=={{header|Racket}}==
=={{header|Racket}}==


<lang racket>#lang racket/base
<syntaxhighlight lang="racket">#lang racket/base


(require racket/hash)
(require racket/hash)
Line 1,298: Line 1,382:


(hash-union base-data update-data #:combine (λ (a b) b)))
(hash-union base-data update-data #:combine (λ (a b) b)))
</syntaxhighlight>
</lang>


{{out}}
{{out}}
Line 1,308: Line 1,392:
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.
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.


<lang perl6># Show original hashes
<syntaxhighlight lang="raku" line># Show original hashes
say my %base = :name('Rocket Skates'), :price<12.75>, :color<yellow>;
say my %base = :name('Rocket Skates'), :price<12.75>, :color<yellow>;
say my %update = :price<15.25>, :color<red>, :year<1974>;
say my %update = :price<15.25>, :color<red>, :year<1974>;
Line 1,323: Line 1,407:


# Demonstrate unmutated hashes
# Demonstrate unmutated hashes
say "\n", %base, "\n", %update;</lang>
say "\n", %base, "\n", %update;</syntaxhighlight>
{{out}}
{{out}}
<pre>{color => yellow, name => Rocket Skates, price => 12.75}
<pre>{color => yellow, name => Rocket Skates, price => 12.75}
Line 1,363: Line 1,447:
The double quotes that surround the string (character) items (as shown in the task's preamble) weren't included for this presentation,
The double quotes that surround the string (character) items (as shown in the task's preamble) weren't included for this presentation,
<br>although they could easily be added.
<br>although they could easily be added.
<lang rexx>/*REXX program merges two associative arrays (requiring an external list of indices). */
<syntaxhighlight lang="rexx">/*REXX program merges two associative arrays (requiring an external list of indices). */
$.= /*define default value(s) for arrays. */
$.= /*define default value(s) for arrays. */
@.wAAn= 21; @.wKey= 7; @.wVal= 7 /*max widths of: AAname, keys, values.*/
@.wAAn= 21; @.wKey= 7; @.wVal= 7 /*max widths of: AAname, keys, values.*/
Line 1,395: Line 1,479:
say center(AAn, @.wAAn) right(key, @.wKey) $.AAn.key /*show some information.*/
say center(AAn, @.wAAn) right(key, @.wKey) $.AAn.key /*show some information.*/
end /*j*/
end /*j*/
return</lang>
return</syntaxhighlight>
{{out|output|text=&nbsp; when using the internal default inputs:}}
{{out|output|text=&nbsp; when using the internal default inputs:}}
<pre>
<pre>
Line 1,416: Line 1,500:


=={{header|Ring}}==
=={{header|Ring}}==
<lang ring>
<syntaxhighlight lang="ring">
load "stdlib.ring"
load "stdlib.ring"


Line 1,440: Line 1,524:
see list1[n][1] + " = " + list1[n][2] + nl
see list1[n][1] + " = " + list1[n][2] + nl
next
next
</syntaxhighlight>
</lang>
Output:
Output:
<pre>
<pre>
Line 1,450: Line 1,534:


=={{header|Ruby}}==
=={{header|Ruby}}==
<lang ruby>base = {"name" => "Rocket Skates", "price" => 12.75, "color" => "yellow"}
<syntaxhighlight lang="ruby">base = {"name" => "Rocket Skates", "price" => 12.75, "color" => "yellow"}
update = {"price" => 15.25, "color" => "red", "year" => 1974}
update = {"price" => 15.25, "color" => "red", "year" => 1974}


result = base.merge(update)
result = base.merge(update)
p result</lang>
p result</syntaxhighlight>
{{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|Rust}}==
=={{header|Rust}}==
<lang rust>use std::collections::HashMap;
<syntaxhighlight lang="rust">use std::collections::HashMap;


fn main() {
fn main() {
Line 1,476: Line 1,560:
println!("{:#?}", original);
println!("{:#?}", original);
}
}
</syntaxhighlight>
</lang>
{{output}}
{{output}}
<pre>{
<pre>{
Line 1,484: Line 1,568:
"price": "15.25",
"price": "15.25",
}</pre>
}</pre>
=={{header|Scheme}}==
{{works with|Chez Scheme}}
'''Quick and Dirty'''
<br />
The procedures which access association lists (alist) in Scheme prefer the first matched item
in an alist. Hence, a merge may be implemented by simply appending the update alist to the
front of the base alist. The downside is this leaves a bunch of useless junk in the alist.
<syntaxhighlight lang="scheme">; Merge alists by appending the update list onto the front of the base list.
; (The extra '() is so that append doesn't co-opt the second list.)
(define append-alists
(lambda (base update)
(append update base '())))

; Test...
(printf "~%Merge using append procedure...~%")
; The original base and update alists.
(let ((base '(("name" . "Rocket Skates") ("price" . 12.75) ("color" . "yellow" )))
(update '(("price" . 15.25) ("color" . "red") ("year" . 1974))))
; Merge by appending the update list onto the front of the base list.
(let ((merged (append-alists base update)))
; Show that everything worked.
(printf "Merged alist:~%~s~%" merged)
(printf "Values from merged alist:~%")
(let loop ((keys '("name" "price" "color" "year")))
(unless (null? keys)
(printf "~s -> ~s~%" (car keys) (cdr (assoc (car keys) merged)))
(loop (cdr keys))))))</syntaxhighlight>
{{out}}
<pre>
Merge using append procedure...
Merged alist:
(("price" . 15.25) ("color" . "red") ("year" . 1974) ("name" . "Rocket Skates") ("price" . 12.75) ("color" . "yellow"))
Values from merged alist:
"name" -> "Rocket Skates"
"price" -> 15.25
"color" -> "red"
"year" -> 1974
</pre>
'''More True to Intent'''
<br />
This is more true to the intent of the Task. It generates a new alist with only the key/value
pairs needed.
<syntaxhighlight lang="scheme">; Merge the given alists. Prefer the items from the "update" alist.
; Returns a new list; the argument lists are not modified.
(define merge-alists
(lambda (base update)
(let ((merged (list-copy update))
(remains (list-copy base)))
(let loop ((shadowing merged))
(if (null? shadowing)
(append merged remains)
(begin (set! remains (remp (lambda (pair) (equal? (car pair) (caar shadowing)))
remains))
(loop (cdr shadowing))))))))

; Test...
(printf "~%Merge using defined merge-alists procedure...~%")
; The original base and update alists.
(let ((base '(("name" . "Rocket Skates") ("price" . 12.75) ("color" . "yellow" )))
(update '(("price" . 15.25) ("color" . "red") ("year" . 1974))))
; Merge using the defined merge-alists procedure.
(let ((merged (merge-alists base update)))
; Show that everything worked.
(printf "Merged alist:~%~s~%" merged)
(printf "Values from merged alist:~%")
(let loop ((keys '("name" "price" "color" "year")))
(unless (null? keys)
(printf "~s -> ~s~%" (car keys) (cdr (assoc (car keys) merged)))
(loop (cdr keys))))))</syntaxhighlight>
{{out}}
<pre>
Merge using defined merge-alists procedure...
Merged alist:
(("price" . 15.25) ("color" . "red") ("year" . 1974) ("name" . "Rocket Skates"))
Values from merged alist:
"name" -> "Rocket Skates"
"price" -> 15.25
"color" -> "red"
"year" -> 1974
</pre>


=={{header|SenseTalk}}==
=={{header|SenseTalk}}==
In addition to setting individual property values, SenseTalk includes 5 built-in operations for modifying property lists, to: add properties, replace properties, remove properties, retain properties, and rename properties. The operation to replace properties is needed for this task.
In addition to setting individual property values, SenseTalk includes 5 built-in operations for modifying property lists, to: add properties, replace properties, remove properties, retain properties, and rename properties. The operation to replace properties is needed for this task.
<lang sensetalk>set base to {name:"Rocket Skates", price:12.75, color:"yellow"}
<syntaxhighlight lang="sensetalk">set base to {name:"Rocket Skates", price:12.75, color:"yellow"}


set update to {price:15.25, color:"red", year:1974}
set update to {price:15.25, color:"red", year:1974}
Line 1,500: Line 1,664:
replace properties of update in base
replace properties of update in base
put "Base after update: " & base
put "Base after update: " & base
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 1,510: Line 1,674:


=={{header|Smalltalk}}==
=={{header|Smalltalk}}==
<lang smalltalk>base := Dictionary withAssociations:{
<syntaxhighlight lang="smalltalk">base := Dictionary withAssociations:{
'name'-> 'Rocket Skates' .
'name'-> 'Rocket Skates' .
'price' -> 12.75 .
'price' -> 12.75 .
Line 1,524: Line 1,688:
declareAllFrom:update.
declareAllFrom:update.


Transcript showCR: result.</lang>
Transcript showCR: result.</syntaxhighlight>
{{out}}
{{out}}
<pre>Dictionary(name->Rocket Skates price->15.25 year->1974 color->red)</pre>
<pre>Dictionary(name->Rocket Skates price->15.25 year->1974 color->red)</pre>
Line 1,530: Line 1,694:
=={{header|Swift}}==
=={{header|Swift}}==
{{works with|Swift|5+}}
{{works with|Swift|5+}}
<lang swift>let base : [String: Any] = ["name": "Rocket Skates", "price": 12.75, "color": "yellow"]
<syntaxhighlight lang="swift">let base : [String: Any] = ["name": "Rocket Skates", "price": 12.75, "color": "yellow"]
let update : [String: Any] = ["price": 15.25, "color": "red", "year": 1974]
let update : [String: Any] = ["price": 15.25, "color": "red", "year": 1974]


let result = base.merging(update) { (_, new) in new }
let result = base.merging(update) { (_, new) in new }


print(result)</lang>
print(result)</syntaxhighlight>
{{output}}
{{output}}
<pre>["price": 15.25, "name": "Rocket Skates", "color": "red", "year": 1974]</pre>
<pre>["price": 15.25, "name": "Rocket Skates", "color": "red", "year": 1974]</pre>


=={{header|Tcl}}==
=={{header|Tcl}}==
<lang tcl>set dict1 [dict create name "Rocket Skates" price 12.75 color yellow]
<syntaxhighlight lang="tcl">set dict1 [dict create name "Rocket Skates" price 12.75 color yellow]
set dict2 [dict create price 15.25 color red year 1974]
set dict2 [dict create price 15.25 color red year 1974]
dict for {key val} [dict merge $dict1 $dict2] {
dict for {key val} [dict merge $dict1 $dict2] {
puts "$key: $val"
puts "$key: $val"
}</lang>
}</syntaxhighlight>
{{output}}
{{output}}
<pre>name: Rocket Skates
<pre>name: Rocket Skates
Line 1,554: Line 1,718:
3 ways to do this tasks :
3 ways to do this tasks :
First : With Arrays + Type
First : With Arrays + Type
<syntaxhighlight lang="vb">
<lang vb>
Private Type Associative
Private Type Associative
Key As String
Key As String
Line 1,610: Line 1,774:
Next i
Next i
Debug.Print "-----------------------------"
Debug.Print "-----------------------------"
End Sub</lang>
End Sub</syntaxhighlight>


Second way (simply) : with the Scripting Dictionary Object :
Second way (simply) : with the Scripting Dictionary Object :
<lang vb>Sub Main_With_Dictionary()
<syntaxhighlight lang="vb">Sub Main_With_Dictionary()
Dim Base As Object, Update As Object, Merged As Object, K As Variant
Dim Base As Object, Update As Object, Merged As Object, K As Variant
'INIT VARIABLE
'INIT VARIABLE
Line 1,635: Line 1,799:
Debug.Print K, Merged(K)
Debug.Print K, Merged(K)
Next K
Next K
End Sub</lang>
End Sub</syntaxhighlight>


And the Third : With a Class Module (named ClassArrayAssociative)
And the Third : With a Class Module (named ClassArrayAssociative)
The Class Module code:
The Class Module code:
<lang vb>Option Explicit
<syntaxhighlight lang="vb">Option Explicit


Private mKey As String
Private mKey As String
Line 1,660: Line 1,824:
Value = V
Value = V
Key = K
Key = K
End Sub</lang>
End Sub</syntaxhighlight>
The Module code :
The Module code :
<lang vb>Sub Main_With_Class()
<syntaxhighlight lang="vb">Sub Main_With_Class()
Dim Base(2) As New ClassArrayAssociative, Up(2) As New ClassArrayAssociative
Dim Base(2) As New ClassArrayAssociative, Up(2) As New ClassArrayAssociative
ReDim Result(UBound(Base)) As New ClassArrayAssociative
ReDim Result(UBound(Base)) As New ClassArrayAssociative
Line 1,704: Line 1,868:
Next i
Next i
Debug.Print "-----------------------------"
Debug.Print "-----------------------------"
End Sub</lang>
End Sub</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,714: Line 1,878:
-----------------------------</pre>
-----------------------------</pre>


=={{header|VBScript}}==
<syntaxhighlight lang="vb">
set d1=createobject("Scripting.Dictionary")
d1.add "name", "Rocket Skates"
d1.add "price", 12.75
d1.add "color", "yellow"


set d2=createobject("Scripting.Dictionary")
d2.add "price", 15.25
d2.add "color", "red"
d2.add "year", 1974

set d3=createobject("Scripting.Dictionary")
for each k1 in d1.keys
if not d3.exists(k1) then
d3.add k1, d1(k1)
else
d3(k1)=d1(k1)
end if
next
for each k2 in d2.keys
if not d3.exists(k2) then
d3.add k2, d2(k2)
else
d3(k2)=d2(k2)
end if
next

for each k3 in d3.keys
wscript.echo k3 & vbtab & d3(k3)
next
</syntaxhighlight>
{{out}}
<pre>
name Rocket Skates
price 15.25
color red
year 1974
</pre>

=={{header|V (Vlang)}}==
<syntaxhighlight lang="v (vlang)">type Generic = int|string|f64
type Assoc = map[string]Generic

fn merge(base Assoc, update Assoc) Assoc {
mut result := Assoc(map[string]Generic{})
for k, v in base {
result[k] = v
}
for k, v in update {
result[k] = v
}
return result
}
fn main() {
base := Assoc({"name": Generic("Rocket Skates"), "price": 12.75, "color": "yellow"})
update := Assoc({"price": Generic(15.25), "color": "red", "year": 1974})
result := merge(base, update)
for k,v in result {
println('$k: $v')
}
}</syntaxhighlight>
{{out}}
<pre>
name: Generic('Rocket Skates')
price: Generic(15.25)
color: Generic('red')
year: Generic(1974)
</pre>


=={{header|Wren}}==
=={{header|Wren}}==
<lang ecmascript>var mergeMaps = Fn.new { |m1, m2|
<syntaxhighlight lang="wren">var mergeMaps = Fn.new { |m1, m2|
var m3 = {}
var m3 = {}
for (key in m1.keys) m3[key] = m1[key]
for (key in m1.keys) m3[key] = m1[key]
Line 1,727: Line 1,960:
var update = { "price": 15.25, "color": "red", "year": 1974 }
var update = { "price": 15.25, "color": "red", "year": 1974 }
var merged = mergeMaps.call(base, update)
var merged = mergeMaps.call(base, update)
System.print(merged)</lang>
System.print(merged)</syntaxhighlight>


{{out}}
{{out}}
Line 1,735: Line 1,968:


=={{header|zkl}}==
=={{header|zkl}}==
<lang zkl>base:=Dictionary(
<syntaxhighlight lang="zkl">base:=Dictionary(
"name", "Rocket Skates",
"name", "Rocket Skates",
"price", 12.75,
"price", 12.75,
Line 1,746: Line 1,979:
update.pump( new:=base.copy() );
update.pump( new:=base.copy() );


new.pump(Void,fcn([(k,v)]){ println("%s\t%s".fmt(k,v)) });</lang>
new.pump(Void,fcn([(k,v)]){ println("%s\t%s".fmt(k,v)) });</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>

Latest revision as of 18:37, 17 February 2024

Task
Associative array/Merging
You are encouraged to solve this task according to the task description, using any language you may know.
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



11l

Translation of: Python
V base = [‘name’ = ‘Rocket Skates’, ‘price’ = ‘12.75’, ‘color’ = ‘yellow’]
V update = [‘price’ = ‘15.25’, ‘color’ = ‘red’, ‘year’ = ‘1974’]

V result = copy(base)
result.update(update)

print(result)
Output:
[color = red, name = Rocket Skates, price = 15.25, year = 1974]

Ada

with Ada.Text_Io;
with Ada.Containers.Indefinite_Ordered_Maps;

procedure Merge_Maps is
   use Ada.Text_Io;

   type Key_Type   is new String;
   type Value_Type is new String;

   package Maps is
     new Ada.Containers.Indefinite_Ordered_Maps (Key_Type     => Key_Type,
                                                 Element_Type => Value_Type);
   use Maps;

   function Merge (Original : Map; Update : Map) return Map is
      Result : Map    := Original;
      Cur    : Cursor := Update.First;
   begin
      while Has_Element (Cur) loop
         if Original.Contains (Key (Cur)) then
            Result.Replace_Element (Result.Find (Key (Cur)),
                                    Element (Cur));
         else
            Result.Insert (Key (Cur), Element (Cur));
         end if;
         Next (Cur);
      end loop;
      return Result;
   end Merge;

   procedure Put_Map (M : Map) is
      Cur : Cursor := M.First;
   begin
      while Has_Element (Cur) loop
         Put (String (Key (Cur)));
         Set_Col (12);
         Put (String (Element (Cur)));
         New_Line;
         Next (Cur);
      end loop;
   end Put_Map;

   Original : Map;
   Update   : Map;
   Result   : Map;
begin
   Original.Insert ("name", "Rocket Skates");
   Original.Insert ("price", "12.75");
   Original.Insert ("color", "yellow");

   Update.Insert ("price", "15.25");
   Update.Insert ("color", "red");
   Update.Insert ("year", "1974");

   Result := Merge (Original, Update);

   Put_Line ("Original:");
   Put_Map (Original);
   New_Line;

   Put_Line ("Update:");
   Put_Map (Update);
   New_Line;

   Put_Line ("Result of merge:");
   Put_Map (Result);
   New_Line;

end Merge_Maps;
Output:
Original:
color      yellow
name       Rocket Skates
price      12.75

Update:
color      red
price      15.25
year       1974

Result of merge:
color      red
name       Rocket Skates
price      15.25
year       1974

ALGOL 68

Works with: ALGOL 68G version Any - tested with release 2.8.3.win32

Uses the associative array implementations in ALGOL_68/prelude.

# associative array merging                                                  #
 
# the modes allowed as associative array element values - change to suit     #
MODE AAVALUE = UNION( STRING, INT, REAL );
# the modes allowed as associative array element keys - change to suit       #
MODE AAKEY   = STRING;
# initial value for an array element                                         #
AAVALUE init element value = "";
 
# include the associative array code                                         #
PR read "aArrayBase.a68" PR
 
# adds or replaces all elements from b into a                     #
PRIO UPDATE = 9;
OP UPDATE = ( REF AARRAY a, REF AARRAY b )REF AARRAY:
BEGIN
    REF AAELEMENT e := FIRST b;
    WHILE e ISNT nil element DO
        a // key OF e := value OF e;
        e := NEXT b
    OD;
    a
END # UPDATE # ;
 
# construct the associative arrays for the task                   #
REF AARRAY a := INIT LOC AARRAY;
REF AARRAY b := INIT LOC AARRAY;
a // "name"  := "Rocket Skates";
a // "price" := 12.75;
a // "color" := "yellow";
b // "price" := 15.25;
b // "color" := "red";
b // "year"  := 1974;
# merge the arrays                                                #
REF AARRAY c := INIT LOC AARRAY;
c UPDATE a UPDATE b;
# show the merged array                                           #
REF AAELEMENT e := FIRST c;
WHILE e ISNT nil element DO
    print( ( key OF e
           , ": "
           , CASE value OF e
               IN (STRING s): s
                , (INT    i): whole( i, 0 )
                , (REAL   r): fixed( r, -12, 2 )
              OUT "????"
             ESAC
           , newline
           )
         );
    e := NEXT c
OD
Output:
name: Rocket Skates
year: 1974
price:        15.25
color: red

AppleScript

The method with AppleScript "records" is to concatenate them. The result is a third record containing the labels from both contributors, the values from the record to the left of the operator being kept where labels are shared. The bars with some of the labels below are to distinguish them as "user" labels from tokenised ones that are provided as standards for use by scriptable applications and scripting additions. However, merging records works with tokenised labels too.

set baseRecord to {|name|:"Rocket Skates", price:12.75, |color|:"yellow"}
set updateRecord to {price:15.25, |color|:"red", |year|:1974}

set mergedRecord to updateRecord & baseRecord
return mergedRecord
Output:
{price:15.25, |color|:"red", |year|:1974, |name|:"Rocket Skates"}

Arturo

details: #[name: "Rocket Skates" price: 12.75 colour: 'yellow]
newDetails: extend details #[price: 15.25 colour: 'red year: 1974]

print newDetails
Output:
[name:Rocket Skates price:15.25 colour:red year:1974]

AutoHotkey

merge(base, update){
	Merged  := {}
	for k, v in base
		Merged[k] := v
	for k, v in update
		Merged[k] := v
	return Merged
}

Examples:

base := {"name":"Rocket Skates", "price":12.75, "color":"yellow"}
update := {"price":15.25, "color":"red", "year":1974}
Merged := merge(base, update)
for k, v in Merged
	result .= k " : " v "`n"
MsgBox % result

Outputs:

color : red
name : Rocket Skates
price : 15.25
year : 1974

AWK

# syntax: GAWK -f ASSOCIATIVE_ARRAY_MERGING.AWK
#
# sorting:
#   PROCINFO["sorted_in"] is used by GAWK
#   SORTTYPE is used by Thompson Automation's TAWK
#
BEGIN {
    PROCINFO["sorted_in"] = "@ind_str_asc" ; SORTTYPE = 1
    arr1["name"] = "Rocket Skates"
    arr1["price"] = "12.75"
    arr1["color"] = "yellow"
    show_array(arr1,"base")
    arr2["price"] = "15.25"
    arr2["color"] = "red"
    arr2["year"] = "1974"
    show_array(arr2,"update")
    for (i in arr1) { arr3[i] = arr1[i] }
    for (i in arr2) { arr3[i] = arr2[i] }
    show_array(arr3,"merged")
    exit(0)
}
function show_array(arr,desc,  i) {
    printf("\n%s array\n",desc)
    for (i in arr) {
      printf("%-5s : %s\n",i,arr[i])
    }
}
Output:
base array
color : yellow
name  : Rocket Skates
price : 12.75

update array
color : red
price : 15.25
year  : 1974

merged array
color : red
name  : Rocket Skates
price : 15.25
year  : 1974

B4X

Dim m1 As Map = CreateMap("name": "Rocket Skates", "price": 12.75, "color": "yellow")
Dim m2 As Map = CreateMap("price": 15.25, "color": "red", "year": 1974)
Dim merged As Map
merged.Initialize
For Each m As Map In Array(m1, m2)
	For Each key As Object In m.Keys
		merged.Put(key, m.Get(key))
	Next
Next
Log(merged)

BASIC

BaCon

DECLARE base$, update$, merge$ ASSOC STRING

base$("name") = "Rocket Skates"
base$("price") = "12.75"
base$("color") = "yellow"

PRINT "Base array"
FOR x$ IN OBTAIN$(base$)
    PRINT x$, " : ", base$(x$)
NEXT

update$("price") = "15.25"
update$("color") = "red"
update$("year") = "1974"

PRINT NL$, "Update array"
FOR x$ IN OBTAIN$(update$)
    PRINT x$, " : ", update$(x$)
NEXT

merge$() = base$()
merge$() = update$()

PRINT NL$, "Merged array"
FOR x$ IN OBTAIN$(merge$)
    PRINT x$, " : ", merge$(x$)
NEXT
Output:
Base array
name : Rocket Skates
color : yellow
price : 12.75

Update array
year : 1974
color : red
price : 15.25

Merged array
name : Rocket Skates
year : 1974
color : red
price : 15.25

C++

#include <iostream>
#include <string>
#include <map>

template<typename map_type>
map_type merge(const map_type& original, const map_type& update) {
    map_type result(update);
    result.insert(original.begin(), original.end());
    return result;
}

int main() {
    typedef std::map<std::string, std::string> map;
    map original{
        {"name", "Rocket Skates"},
        {"price", "12.75"},
        {"color", "yellow"}
    };
    map update{
        {"price", "15.25"},
        {"color", "red"},
        {"year", "1974"}
    };
    map merged(merge(original, update));
    for (auto&& i : merged)
        std::cout << "key: " << i.first << ", value: " << i.second << '\n';
    return 0;
}
Output:
key: color, value: red
key: name, value: Rocket Skates
key: price, value: 15.25
key: year, value: 1974

C#

using System;
using System.Collections.Generic;
using System.Linq;

public class Program
{
    public static void Main() {
        var baseData = new Dictionary<string, object> {
            ["name"] = "Rocket Skates",
            ["price"] = 12.75,
            ["color"] = "yellow"
        };
        var updateData = new Dictionary<string, object> {
            ["price"] = 15.25,
            ["color"] = "red",
            ["year"] = 1974
        };
        var mergedData = new Dictionary<string, object>();
        foreach (var entry in baseData.Concat(updateData)) {
            mergedData[entry.Key] = entry.Value;
        }
        foreach (var entry in mergedData) {
            Console.WriteLine(entry);
        }
   }
}
Output:
[name, Rocket Skates]
[price, 15.25]
[color, red]
[year, 1974]

Clojure

(defn update-map [base update]
  (merge base update))

  (update-map {"name"  "Rocket Skates"
               "price" "12.75"
               "color" "yellow"}
              {"price" "15.25"
               "color" "red"
               "year"  "1974"})
Output:
{"name" "Rocket Skates", "price" "15.25", "color" "red", "year" "1974"}

Crystal

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

puts base.merge(update)
Output:
{"name" => "Rocket Skates", "price" => 15.25, "color" => "red", "year" => 1974}

Common Lisp

In Common Lisp, the value of a key in an alist or a plist is defined as the first value in the list with a matching key or indicator. Thus, all that is necessary to implement this algorithm for either is the following:

(append list2 list1)

These implementations for alists and plists are more complicated, but avoid duplicate keys in the results:

(defun merge-alists (alist1 alist2)
  (nconc
   (loop :for pair1 :in alist1
         :for pair2 := (assoc (car pair1) alist2)
         :do (setf alist2 (remove pair2 alist2))
         :collect (or pair2 pair1))
   alist2))

(defun merge-plists (plist1 plist2)
  (let ((res '()))
    (loop :for (key val) :on plist1 :by #'cddr
          :do (setf (getf res key) val))
    (loop :for (key val) :on plist2 :by #'cddr
          :do (setf (getf res key) val))
    res))

Dart

main() {
	var base = {
		'name':   'Rocket Skates',
		'price':  12.75,
		'color':  'yellow'
	};

	var newData = {
		'price': 15.25,
		'color': 'red',
		'year':  1974
	};

	var updated = Map.from( base ) // create new Map from base
		..addAll( newData ); // use cascade operator to add all new data
	
	assert( base.toString()    == '{name: Rocket Skates, price: 12.75, color: yellow}' );
	assert( updated.toString() == '{name: Rocket Skates, price: 15.25, color: red, year: 1974}');


}

Delphi

Translation of: C#
program Associative_arrayMerging;

{$APPTYPE CONSOLE}

uses
  System.Generics.Collections;

type
  TData = TDictionary<string, Variant>;

var
  baseData, updateData, mergedData: TData;
  entry: string;

begin
  baseData := TData.Create();
  baseData.Add('name', 'Rocket Skates');
  baseData.Add('price', 12.75);
  baseData.Add('color', 'yellow');

  updateData := TData.Create();
  updateData.Add('price', 15.25);
  updateData.Add('color', 'red');
  updateData.Add('year', 1974);

  mergedData := TData.Create();
  for entry in baseData.Keys do
    mergedData.AddOrSetValue(entry, baseData[entry]);

  for entry in updateData.Keys do
    mergedData.AddOrSetValue(entry, updateData[entry]);

  for entry in mergedData.Keys do
    Writeln(entry, ' ', mergedData[entry]);

  mergedData.Free;
  updateData.Free;
  baseData.Free;

  Readln;
end.
Output:
price 15,25
year 1974
color red
name Rocket Skates

Elixir

Elixir has a built-in hashmap type, called Map.

base = %{name: "Rocket Skates", price: 12.75, color: "yellow"}
update = %{price: 15.25, color: "red", year: 1974}
result = Map.merge(base, update)
IO.inspect(result)
Output:
%{color: "red", name: "Rocket Skates", price: 15.25, year: 1974}

The above sample uses atoms as the key type. If strings are needed, the base map would look like this:

base = %{"name" => "Rocket Skates", "price" => 12.75, "color" => "yellow"}

F#

type N = |Price of float|Name of string|Year of int|Colour of string
let n=Map<string,N>[("name",Name("Rocket Skates"));("price",Price(12.75));("colour",Colour("yellow"))]
let g=Map<string,N>[("price",Price(15.25));("colour",Colour("red"));("year",Year(1974))]
let ng=(Map.toList n)@(Map.toList g)|>Map.ofList
printfn "%A" ng
Output:
map
  [("colour", Colour "red"); ("name", Name "Rocket Skates");
   ("price", Price 15.25); ("year", Year 1974)]

// Minimum positive multiple in base 10 using only 0 and 1. Nigel Galloway: March 9th., 2020

Factor

The assoc-union word does this. assoc-union! is a variant that mutates the first associative array.

Works with: Factor version 0.99 2019-10-06
USING: assocs prettyprint ;

{ { "name" "Rocket Skates" } { "price" 12.75 } { "color" "yellow" } }
{ { "price" 15.25 } { "color" "red" } { "year" 1974 } }
assoc-union .
Output:
V{
    { "name" "Rocket Skates" }
    { "price" 15.25 }
    { "color" "red" }
    { "year" 1974 }
}

FutureBasic

void local fn DoIt
  CFDictionaryRef base = @{@"name" :@"Rocket Skates", @"price":@12.75, @"color":@"yellow"}
  CFDictionaryRef update = @{@"price":@15.25, @"color":@"red", @"year":@1974}
  
  CFMutableDictionaryRef merged = fn MutableDictionaryWithDictionary( base )
  MutableDictionaryAddEntriesFromDictionary( merged, update )
  
  print merged
end fn

fn DoIt

HandleEvents


Go

package main

import "fmt"

type assoc map[string]interface{}

func merge(base, update assoc) assoc {
    result := make(assoc)
    for k, v := range base {
        result[k] = v
    }
    for k, v := range update {
        result[k] = v
    }
    return result
}

func main() {
    base := assoc{"name": "Rocket Skates", "price": 12.75, "color": "yellow"}
    update := assoc{"price": 15.25, "color": "red", "year": 1974}
    result := merge(base, update)
    fmt.Println(result)
}
Output:
map[color:red name:Rocket Skates price:15.25 year:1974]

Haskell

There are various approaches, all of which would be strictly typed.
For example, if we want to treat the input and output records as all sharing the same type:

data Item = Item
  { name :: Maybe String
  , price :: Maybe Float
  , color :: Maybe String
  , year :: Maybe Int
  } deriving (Show)

itemFromMerge :: Item -> Item -> Item
itemFromMerge (Item n p c y) (Item n1 p1 c1 y1) =
  Item (maybe n pure n1) (maybe p pure p1) (maybe c pure c1) (maybe y pure y1)

main :: IO ()
main =
  print $
  itemFromMerge
    (Item (Just "Rocket Skates") (Just 12.75) (Just "yellow") Nothing)
    (Item Nothing (Just 15.25) (Just "red") (Just 1974))
Output:
Item {name = Just "Rocket Skates", price = Just 15.25, color = Just "red", year = Just 1974}

Icon and Unicon

procedure main()
    local base, update, master, f, k

    base := table()
    base["name"] := "Rocket Skates"
    base["price"] := 12.75
    base["color"] := "yellow"

    update := table()
    update["price"] := 15.25
    update["color"] := "red"
    update["year"] := 1974

    master := table()
    every k := key((f := base | update)) do {
        master[k] := f[k]
    }

    every k := key(master) do {
        write(k, " = ", master[k])
    }
end
Output:
prompt$ unicon -s merge.icn -x
color = red
name = Rocket Skates
price = 15.25
year = 1974

J

merge=: ,.    NB. use: update merge original
compress=: #"1~ ~:@:keys
keys=: {.
values=: {:
get=: [: > ((i.~ keys)~ <)~ { values@:]   NB. key get (associative array)
pair=: [: |: <;._2;._2

Exercise the definitions. Interactive J prompts with 3 space indentation.

   ] D=: pair 0 :0
name;Rocket Skates;
price;12.75;
color;yellow;
)
┌─────────────┬─────┬──────┐
│name         │price│color │
├─────────────┼─────┼──────┤
│Rocket Skates│12.75│yellow│
└─────────────┴─────┴──────┘

   E=: pair 0 :0
price;15.25;
color;red;
year;1974;
)

   'color'get D
yellow

   'color'get E
red

   ] F=: E merge D
┌─────┬─────┬────┬─────────────┬─────┬──────┐
│price│color│year│name         │price│color │
├─────┼─────┼────┼─────────────┼─────┼──────┤
│15.25│red  │1974│Rocket Skates│12.75│yellow│
└─────┴─────┴────┴─────────────┴─────┴──────┘

   'color' get F
red

   ]G=: compress F
┌─────┬─────┬────┬─────────────┐
│price│color│year│name         │
├─────┼─────┼────┼─────────────┤
│15.25│red  │1974│Rocket Skates│
└─────┴─────┴────┴─────────────┘

   'no such key'get F
|index error: get
|   'no such key'    get F

   'color'get F
red

Java

See also, Java - Associative array/Creation.

This task illustrates the difference between statically typed languages, and dynamically typed.
With exception to the var keyword, Java is statically typed. So, using an undefined, non-static, value, creates a trivial situation.

A good way to understand this task is that it's not possible in a static-typed language.
It defeats the purpose of defining the data-type.
For Java, if you're going to use Object as your data-type, you need to re-evaluate the method of abstraction.

Java offers generics, interfaces, and abstract classes for this task.

Considering this, to complete this specific task, I would just store the values as strings.

import java.util.LinkedHashMap;
import java.util.Map;
Map<String, String> mapA = new LinkedHashMap<>();
mapA.put("name", "Rocket Skates");
mapA.put("price", "12.75");
mapA.put("color", "yellow");

Map<String, String> mapB = new LinkedHashMap<>();
mapB.put("price", "15.25");
mapB.put("color", "red");
mapB.put("year", "1974");

Map<String, String> mapC = new LinkedHashMap<>();
mapC.putAll(mapA);
mapC.putAll(mapB);

To show that the original maps are not affected.

for(Map.Entry<String, String> entry : mapA.entrySet())
    System.out.printf("%-20s%s%n", entry.getKey(), entry.getValue());

for(Map.Entry<String, String> entry : mapB.entrySet())
    System.out.printf("%-20s%s%n", entry.getKey(), entry.getValue());

for(Map.Entry<String, String> entry : mapC.entrySet())
    System.out.printf("%-20s%s%n", entry.getKey(), entry.getValue());
name                Rocket Skates
price               12.75
color               yellow
price               15.25
color               red
year                1974
name                Rocket Skates
price               15.25
color               red
year                1974

While not recommended, due to scalability, if you did want to use an Object as the value, you could use the following implementation.
This will produce the same output as above.

Map<String, Object> mapA = new LinkedHashMap<>();
mapA.put("name", "Rocket Skates");
mapA.put("price", 12.75);
mapA.put("color", "yellow");

Map<String, Object> mapB = new LinkedHashMap<>();
mapB.put("price", 15.25);
mapB.put("color", "red");
mapB.put("year", 1974);

Map<String, Object> mapC = new LinkedHashMap<>();
mapC.putAll(mapA);
mapC.putAll(mapB);

JavaScript

(() => {
    'use strict';

    console.log(JSON.stringify(
        Object.assign({}, // Fresh dictionary.
            { // Base.
                "name": "Rocket Skates",
                "price": 12.75,
                "color": "yellow"
            }, { // Update.
                "price": 15.25,
                "color": "red",
                "year": 1974
            }
        ), 
        null, 2
    ))
})();
Output:
{
  "name": "Rocket Skates",
  "price": 15.25,
  "color": "red",
  "year": 1974
}

jq

Mapping “associative array” to “JSON object” in the natural way, the specified operation corresponds exactly to addition in jq, so that if A and B are the first and second objects respectively, then the jq expression `A + B` will yield the required result (as a JSON object).

Julia

julia> dict1 = Dict("name" => "Rocket Skates", "price" => 12.75, "color" => "yellow")
Dict{String,Any} with 3 entries:
  "name"  => "Rocket Skates"
  "price" => 12.75
  "color" => "yellow"

julia> dict2 = Dict("price" => 15.25, "color" => "red", "year" => 1974)
Dict{String,Any} with 3 entries:
  "price" => 15.25
  "year"  => 1974
  "color" => "red"

julia> merge(dict1, dict2)
Dict{String,Any} with 4 entries:
  "name"  => "Rocket Skates"
  "price" => 15.25
  "year"  => 1974
  "color" => "red"

julia> merge(dict2, dict1)
Dict{String,Any} with 4 entries:
  "name"  => "Rocket Skates"
  "price" => 12.75
  "year"  => 1974
  "color" => "yellow"

julia> union(dict1, dict2)
6-element Array{Pair{String,Any},1}:
  "name" => "Rocket Skates"
 "price" => 12.75
 "color" => "yellow"
 "price" => 15.25
  "year" => 1974
 "color" => "red"

Kotlin

fun main() {
    val base = HashMap<String,String>()
    val update =  HashMap<String,String>()

    base["name"] = "Rocket Skates"
    base["price"] = "12.75"
    base["color"] = "yellow"

    update["price"] = "15.25"
    update["color"] = "red"
    update["year"] = "1974"

    val merged = HashMap(base)
    merged.putAll(update)

    println("base: $base")
    println("update: $update")
    println("merged: $merged")
}
Output:
base: {color=yellow, price=12.75, name=Rocket Skates}
update: {color=red, year=1974, price=15.25}
merged: {name=Rocket Skates, color=red, year=1974, price=15.25}

Lua

base = {name="Rocket Skates", price=12.75, color="yellow"}
update = {price=15.25, color="red", year=1974}

--[[ clone the base data ]]--
result = {}
for key,val in pairs(base) do
    result[key] = val
end

--[[ copy in the update data ]]--
for key,val in pairs(update) do
    result[key] = val
end

--[[ print the result ]]--
for key,val in pairs(result) do
    print(string.format("%s: %s", key, val))
end
Output:
price: 15.25
color: red
year: 1974
name: Rocket Skates

Mathematica / Wolfram Language

a1 = <|"name" -> "Rocket Skates", "price" -> 12.75, "color" -> "yellow"|>;
a2 = <|"price" -> 15.25, "color" -> "red", "year" -> 1974|>;
Merge[{a1, a2}, Last]
Output:
<|"name" -> "Rocket Skates", "price" -> 15.25, "color" -> "red", "year" -> 1974|>

MiniScript

MiniScript supports merging maps with the + operator.

base = {"name":"Rocket Skates", "price":12.75, "color":"yellow"}
update = {"price":15.25, "color":"red", "year":1974}

result = base + update

print result
Output:
{"color": "red", "name": "Rocket Skates", "price": 15.25, "year": 1974}

Nim

import tables

let t1 = {"name": "Rocket Skates", "price": "12.75", "color": "yellow"}.toTable
let t2 = {"price": "15.25", "color": "red", "year": "1974"}.toTable

var t3 = t1   # Makes a copy.
for key, value in t2.pairs:
  t3[key] = value

echo t3
Output:
{"name": "Rocket Skates", "year": "1974", "color": "red", "price": "15.25"}

Objective-C

Works with: Cocoa
#import <Foundation/Foundation.h>

int main(void) {
  @autoreleasepool {
    NSDictionary *base = @{@"name": @"Rocket Skates", @"price": @12.75, @"color": @"yellow"};
    NSDictionary *update = @{@"price": @15.25, @"color": @"red", @"year": @1974};
    
    NSMutableDictionary *result = [[NSMutableDictionary alloc] initWithDictionary:base];
    [result addEntriesFromDictionary:update];
    
    NSLog(@"%@", result);
  }
  return 0;
}
Output:
{
    color = red;
    name = "Rocket Skates";
    price = "15.25";
    year = 1974;
}

OCaml

Original version by User:Vanyamil

Note that, given OCaml's strong typing, we must declare a type here for the 3-type data. In general, would need the specific data type for the task, or use a PPX rewriter (effectively a compiler middleware) that can rewrite code based on dynamic type extensions.

Helper code for all 3 versions:

type ty = 
    | TFloat of float
    | TInt of int
    | TString of string

type key = string
type assoc = string * ty

let string_of_ty : ty -> string = function
    | TFloat x -> string_of_float x
    | TInt i -> string_of_int i
    | TString s -> s

let print_pair key el =
    Printf.printf "%s: %s\n" key (string_of_ty el)
;;

Association list : naive and functional approach.

let l1 : assoc list = [
    ("name", TString "Rocket Skates");
    ("price", TFloat 12.75);
    ("color", TString "yellow")
] ;;

let l2 : assoc list = [
    ("price", TFloat 15.25);
    ("color", TString "red");
    ("year",  TInt 1974)
] ;;

let rec merge_assoc_list (base_list : assoc list) (add_list : assoc list) : assoc list = 
    List.fold_left
        (fun l (key, val_) -> 
            (key, val_) :: (List.remove_assoc key l)
        )
        base_list
        add_list
;;

let l' = merge_assoc_list l1 l2 ;;

Binary tree/Map functor : proper functional approach.

Works with: OCaml version above 4.03 for union function
module StringMap = Map.Make(String) ;;

let print_map = StringMap.iter print_pair ;;

let map_merge (base : ty StringMap.t) (add : ty StringMap.t) : ty StringMap.t = 
    StringMap.union (fun key v1 v2 -> Some v2) base add
;;

let m1 = StringMap.(
    empty 
    |> add "name" (TString "Rocket Skates")
    |> add "price" (TFloat 12.75)
    |> add "color" (TString "yellow")
) ;;

let m2 = StringMap.(
    empty 
    |> add "price" (TFloat 15.25)
    |> add "color" (TString "red")
    |> add "year" (TInt 1974)
) ;;

let m' = map_merge m1 m2 ;;

print_map m' ;;

Hash table : imperative/mutable approach.

(* Updates the base table with the bindings from add *)
let hash_merge (base : (string, ty) Hashtbl.t) (add : (string, ty) Hashtbl.t) : unit = 
    Hashtbl.iter (Hashtbl.replace base) add

let print_hashtbl t =
    Hashtbl.iter print_pair t

let h1 : (string, ty) Hashtbl.t = Hashtbl.create 10 ;;
Hashtbl.add h1 "name" (TString "Rocket Skates") ;;
Hashtbl.add h1 "price" (TFloat 12.75) ;;
Hashtbl.add h1 "color" (TString "yellow") ;;

let h2 : (string, ty) Hashtbl.t = Hashtbl.create 10 ;;
Hashtbl.add h2 "price" (TFloat 15.25) ;;
Hashtbl.add h2 "color" (TString "red") ;;
Hashtbl.add h2 "year" (TInt 1974) ;;

hash_merge h1 h2 ;;

print_hashtbl h1 ;;

Ol

Since version 3.1.1 function `ff-union` changed from `(ff-union a1 a2 collide)` to `(ff-union collide a1 a2 ...)`!

(define a1 {
   'name    "Rocket Skates"
   'price   12.75
   'color   "yellow"
})

(define a2 {
   'price   15.25
   'color   "red"
   'year    1974 
})

(print "a1: " a1)
(print "a2: " a2)

(define (collide a b) b) ; will use new key value
(print "merged a1 a2: " (ff-union collide a1 a2))
Output:
a1: #ff((name . Rocket Skates) (price . 51/4) (color . yellow))
a2: #ff((price . 61/4) (color . red) (year . 1974))
merged a1 a2: #ff((name . Rocket Skates) (price . 61/4) (color . red) (year . 1974))

Perl

use strict;
use warnings;

my %base = ("name" => "Rocket Skates", "price" => 12.75, "color" => "yellow");
my %more = ("price" => 15.25, "color" => "red", "year" => 1974);

print "Update\n";
my %update = (%base, %more);
printf "%-7s %s\n", $_, $update{$_} for sort keys %update;

print "\nMerge\n";
my %merge;
$merge{$_} = [$base{$_}] for keys %base;
push @{$merge{$_}}, $more{$_} for keys %more;
printf "%-7s %s\n", $_, join ', ', @{$merge{$_}} for sort keys %merge;
Output:
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

Phix

with javascript_semantics
integer d1 = new_dict({{"name","Rocket Skates"},{"price",12.75},{"color","yellow"}}),
        d2 = new_dict({{"price",15.25},{"color","red"},{"year",1974}}),
        d3 = new_dict(d1)
function merger(object key, data, /*user_data*/) setd(key,data,d3) return 1 end function
traverse_dict(merger,NULL,d2)
include builtins/map.e
?pairs(d3)
Output:
{{"color","red"},{"name","Rocket Skates"},{"price",15.25},{"year",1974}}

Phixmonti

include ..\Utilitys.pmt

def scand   /# dict key -- dict n #/
    0 var flag
    var ikey
    len for
        var i
        i 1 2 tolist sget
        ikey == if i var flag exitfor endif
    endfor
    flag
enddef


def getd    /# dict key -- dict data #/
    scand
    dup if get 2 get nip else drop "Unfound" endif
enddef


def setd    /# dict ( key data ) -- dict #/
    1 get rot swap
    scand
    rot swap
    dup if set else put endif
enddef

/# ---------- MAIN ---------- #/

( ( "name" "Rocket Skates" )
  ( "price" 12.75 )
  ( "color" "yellow" ) )

dup
  
( ( "price" 15.25 )
  ( "color" "red" )
  ( "year" 1974 ) )
  
len for
    get rot swap setd swap
endfor

swap

pstack

PHP

<?
$base = array("name" => "Rocket Skates", "price" => 12.75, "color" => "yellow");
$update = array("price" => 15.25, "color" => "red", "year" => 1974);

$result = $update + $base; // Notice that the order is reversed
print_r($result);
?>
Output:
Array
(
    [price] => 15.25
    [color] => red
    [year] => 1974
    [name] => Rocket Skates
)

Alternative:

<?
$base = array("name" => "Rocket Skates", "price" => 12.75, "color" => "yellow");
$update = array("price" => 15.25, "color" => "red", "year" => 1974);

$result = array_merge($base, $update);
print_r($result);
?>
Output:
Array
(
    [name] => Rocket Skates
    [price] => 15.25
    [color] => red
    [year] => 1974
)

PureBasic

NewMap m1.s()
NewMap m2.s()
NewMap m3.s()

m1("name")="Rocket Skates"
m1("price")="12.75"
m1("color")="yellow"

m2("price")="15.25"
m2("color")="red"
m2("year")="1974"

CopyMap(m1(),m3())
 
ForEach m2()
  m3(MapKey(m2()))=m2()
Next

ForEach m3()
  Debug MapKey(m3())+" : "+m3()
Next
Output:
price : 15.25
color : red
year : 1974
name : Rocket Skates

Python

As of Python 3.5, this can be solved with the dictionary unpacking operator.

Works with: Python version 3.5+
base = {"name":"Rocket Skates", "price":12.75, "color":"yellow"}
update = {"price":15.25, "color":"red", "year":1974}

result = {**base, **update}

print(result)
Output:
{'name': 'Rocket Skates', 'price': 15.25, 'color': 'red', 'year': 1974}
Alternative
base = {"name":"Rocket Skates", "price":12.75, "color":"yellow"}
update = {"price":15.25, "color":"red", "year":1974}

result = base.copy()
result.update(update)

print(result)
Output:
{'name': 'Rocket Skates', 'price': 15.25, 'color': 'red', 'year': 1974}
New alternative using '|'
Python 3.9.0 (tags/v3.9.0:9cf6752, Oct  5 2020, 15:34:40) [MSC v.1927 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>> base = {"name":"Rocket Skates", "price":12.75, "color":"yellow"}
>>> update = {"price":15.25, "color":"red", "year":1974}
>>> result = base | update
>>> result
{'name': 'Rocket Skates', 'price': 15.25, 'color': 'red', 'year': 1974}
>>>

Racket

#lang racket/base

(require racket/hash)

(module+ test
  (require rackunit)

  (define base-data (hash "name"	"Rocket Skates"
                          "price"	12.75
                          "color"	"yellow"))

  (define update-data (hash "price"	15.25
                            "color"	"red"
                            "year"	1974))

  (hash-union base-data update-data #:combine (λ (a b) b)))
Output:
'#hash(("color" . "red") ("name" . "Rocket Skates") ("price" . 15.25) ("year" . 1974))

Raku

(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}

REXX

The REXX language has no native support to acquire the keys of an associate array after they have been defined,  
so it must be performed at definition time,   but there are function packages to perform this function.

Some extra coding was added to support adjustable (maximum) widths for the displaying of the associate array names, keys, and values.

The double quotes that surround the string (character) items (as shown in the task's preamble) weren't included for this presentation,
although they could easily be added.

/*REXX program merges two associative arrays  (requiring an external list of indices).  */
$.=                                              /*define default value(s) for arrays.  */
@.wAAn= 21;      @.wKey= 7;       @.wVal= 7      /*max widths of:  AAname, keys, values.*/
call defAA  'base',     "name Rocket Skates",   'price 12.75',   "color yellow"
call defAA  'update',   "price 15.25",          "color red",     'year 1974'
call show   'base'
call show   'update'
call show   'new'
exit                                             /*stick a fork in it,  we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
defAA: procedure expose $. @.;  parse arg AAn;      new= 'new'   /*get AA name; set NEW.*/
         do j=2  to arg();   parse value arg(j)  with  key val   /*obtain key and value.*/
         $.AAn.key= val                          /*assign a  value  to a  key for AAn.  */
         if wordpos(key, $.AAN.?keys)==0  then $.AAn.?keys= $.AAn.?keys key
                                                 /* [↑]  add to key list if not in list.*/
         $.new.key= val                          /*assign a  value  to a  key for "new".*/
         if wordpos(key, $.new.?keys)==0  then $.new.?keys= $.new.?keys key
                                                 /* [↑]  add to key list if not in list.*/
         @.wKey= max(@.wKey, length(key) )       /*find max width of a name of a  key.  */
         @.wVal= max(@.wVal, length(val) )       /*  "   "    "    " "   "   " "  value.*/
         @.wAA = max(@.wAAn, length(AAn) )       /*  "   "    "    " "   "   "    array.*/
         end   /*j*/
       return
/*──────────────────────────────────────────────────────────────────────────────────────*/
show:  procedure expose $. @.;  parse arg AAn;      say;     _= '═'    /*set title char.*/
         do j=1  for words($.AAn.?keys)                                /*process keys.  */
         if j==1  then say  center('associate array', @.wAAn,     _)  ,
                            center("key"            , @.wKey,     _)  ,
                            center('value'          , @.wVal + 2, _)
         key= word($.AAn.?keys, j)                              /*get the name of a key.*/
         say center(AAn, @.wAAn)  right(key, @.wKey)  $.AAn.key /*show some information.*/
         end   /*j*/
       return
output   when using the internal default inputs:
═══associate array═══ ══key══ ═════value═════
        base             name Rocket Skates
        base            price 12.75
        base            color yellow

═══associate array═══ ══key══ ═════value═════
       update           price 15.25
       update           color red
       update            year 1974

═══associate array═══ ══key══ ═════value═════
         new             name Rocket Skates
         new            price 15.25
         new            color red
         new             year 1974

Ring

load "stdlib.ring"

list1 = [:name = "Rocket Skates", :price = 12.75, :color = "yellow"]
list2 = [:price = 15.25, :color = "red", :year = 1974]

for n = 1 to len(list2)
    flag = 0
    for m = 1 to len(list1)
        if list2[n][1] = list1[m][1]
           flag = 1
           del(list1,m)
           add(list1,[list2[n][1],list2[n][2]])
           exit
        ok
    next
    if flag = 0
       add(list1,[list2[n][1],list2[n][2]])
    ok
next

for n = 1 to len(list1)
    see list1[n][1] + " = " + list1[n][2] + nl
next

Output:

name = Rocket Skates
price = 15.25
color = red
year = 1974

Ruby

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

result = base.merge(update)
p result
Output:
{"name"=>"Rocket Skates", "price"=>15.25, "color"=>"red", "year"=>1974}

Rust

use std::collections::HashMap;

fn main() {
    let mut original = HashMap::new();
    original.insert("name", "Rocket Skates");
    original.insert("price", "12.75");
    original.insert("color", "yellow");

    let mut update = HashMap::new();
    update.insert("price", "15.25");
    update.insert("color", "red");
    update.insert("year", "1974");

    original.extend(&update);

    println!("{:#?}", original);
}
Output:
{
    "name": "Rocket Skates",
    "color": "red",
    "year": "1974",
    "price": "15.25",
}

Scheme

Works with: Chez Scheme

Quick and Dirty
The procedures which access association lists (alist) in Scheme prefer the first matched item in an alist. Hence, a merge may be implemented by simply appending the update alist to the front of the base alist. The downside is this leaves a bunch of useless junk in the alist.

; Merge alists by appending the update list onto the front of the base list.
; (The extra '() is so that append doesn't co-opt the second list.)
(define append-alists
  (lambda (base update)
    (append update base '())))

; Test...
(printf "~%Merge using append procedure...~%")
; The original base and update alists.
(let ((base '(("name" . "Rocket Skates") ("price" . 12.75) ("color" . "yellow" )))
      (update '(("price" . 15.25) ("color" . "red") ("year" . 1974))))
  ; Merge by appending the update list onto the front of the base list.
  (let ((merged (append-alists base update)))
    ; Show that everything worked.
    (printf "Merged alist:~%~s~%" merged)
    (printf "Values from merged alist:~%")
    (let loop ((keys '("name" "price" "color" "year")))
      (unless (null? keys)
        (printf "~s -> ~s~%" (car keys) (cdr (assoc (car keys) merged)))
        (loop (cdr keys))))))
Output:
Merge using append procedure...
Merged alist:
(("price" . 15.25) ("color" . "red") ("year" . 1974) ("name" . "Rocket Skates") ("price" . 12.75) ("color" . "yellow"))
Values from merged alist:
"name" -> "Rocket Skates"
"price" -> 15.25
"color" -> "red"
"year" -> 1974

More True to Intent
This is more true to the intent of the Task. It generates a new alist with only the key/value pairs needed.

; Merge the given alists.  Prefer the items from the "update" alist.
; Returns a new list; the argument lists are not modified.
(define merge-alists
  (lambda (base update)
    (let ((merged (list-copy update))
          (remains (list-copy base)))
      (let loop ((shadowing merged))
        (if (null? shadowing)
          (append merged remains)
          (begin (set! remains (remp (lambda (pair) (equal? (car pair) (caar shadowing)))
                                     remains))
                 (loop (cdr shadowing))))))))

; Test...
(printf "~%Merge using defined merge-alists procedure...~%")
; The original base and update alists.
(let ((base '(("name" . "Rocket Skates") ("price" . 12.75) ("color" . "yellow" )))
      (update '(("price" . 15.25) ("color" . "red") ("year" . 1974))))
  ; Merge using the defined merge-alists procedure.
  (let ((merged (merge-alists base update)))
    ; Show that everything worked.
    (printf "Merged alist:~%~s~%" merged)
    (printf "Values from merged alist:~%")
    (let loop ((keys '("name" "price" "color" "year")))
      (unless (null? keys)
        (printf "~s -> ~s~%" (car keys) (cdr (assoc (car keys) merged)))
        (loop (cdr keys))))))
Output:
Merge using defined merge-alists procedure...
Merged alist:
(("price" . 15.25) ("color" . "red") ("year" . 1974) ("name" . "Rocket Skates"))
Values from merged alist:
"name" -> "Rocket Skates"
"price" -> 15.25
"color" -> "red"
"year" -> 1974

SenseTalk

In addition to setting individual property values, SenseTalk includes 5 built-in operations for modifying property lists, to: add properties, replace properties, remove properties, retain properties, and rename properties. The operation to replace properties is needed for this task.

set base to {name:"Rocket Skates", price:12.75, color:"yellow"}

set update to {price:15.25, color:"red", year:1974}

put "Base data: " & base
put "Update data: " & update

// replacing as an operator, to generate merged data on the fly:
put "Merged data: " & base replacing properties in update

// replace as a command, to modify base data in place:
replace properties of update in base
put "Base after update: " & base
Output:
Base data: {color:"yellow", name:"Rocket Skates", price:12.75}
Update data: {color:"red", price:15.25, year:1974}
Merged data: {color:"red", name:"Rocket Skates", price:15.25, year:1974}
Base after update: {color:"red", name:"Rocket Skates", price:15.25, year:1974}

Smalltalk

base := Dictionary withAssociations:{ 
         'name'-> 'Rocket Skates' .
         'price' -> 12.75 .
         'color' -> 'yellow' }.

update := Dictionary withAssociations:{
         'price' -> 15.25 .
         'color' -> 'red' .
         'year' -> 1974 }.
 
result := Dictionary new
             declareAllFrom:base;
             declareAllFrom:update.

Transcript showCR: result.
Output:
Dictionary(name->Rocket Skates price->15.25 year->1974 color->red)

Swift

Works with: Swift version 5+
let base : [String: Any] = ["name": "Rocket Skates", "price": 12.75, "color": "yellow"]
let update : [String: Any] = ["price": 15.25, "color": "red", "year": 1974]

let result = base.merging(update) { (_, new) in new }

print(result)
Output:
["price": 15.25, "name": "Rocket Skates", "color": "red", "year": 1974]

Tcl

set dict1 [dict create name "Rocket Skates" price 12.75 color yellow]
set dict2 [dict create price 15.25 color red year 1974]
dict for {key val} [dict merge $dict1 $dict2] {
    puts "$key: $val"
}
Output:
name: Rocket Skates
price: 15.25
color: red
year: 1974

VBA

3 ways to do this tasks : First : With Arrays + Type

Private Type Associative
    Key As String
    Value As Variant
End Type
Sub Main_Array_Associative()
Dim BaseArray(2) As Associative, UpdateArray(2) As Associative
    FillArrays BaseArray, UpdateArray
    ReDim Result(UBound(BaseArray)) As Associative
    MergeArray Result, BaseArray, UpdateArray
    PrintOut Result
End Sub
Private Sub MergeArray(Res() As Associative, Base() As Associative, Update() As Associative)
Dim i As Long, Respons As Long
    Res = Base
    For i = LBound(Update) To UBound(Update)
        If Exist(Respons, Base, Update(i).Key) Then
            Res(Respons).Value = Update(i).Value
        Else
            ReDim Preserve Res(UBound(Res) + 1)
            Res(UBound(Res)).Key = Update(i).Key
            Res(UBound(Res)).Value = Update(i).Value
        End If
    Next
End Sub
Private Function Exist(R As Long, B() As Associative, K As String) As Boolean
Dim i As Long
    Do
        If B(i).Key = K Then
            Exist = True
            R = i
        End If
        i = i + 1
    Loop While i <= UBound(B) And Not Exist
End Function
Private Sub FillArrays(B() As Associative, U() As Associative)
    B(0).Key = "name"
    B(0).Value = "Rocket Skates"
    B(1).Key = "price"
    B(1).Value = 12.75
    B(2).Key = "color"
    B(2).Value = "yellow"
    U(0).Key = "price"
    U(0).Value = 15.25
    U(1).Key = "color"
    U(1).Value = "red"
    U(2).Key = "year"
    U(2).Value = 1974
End Sub
Private Sub PrintOut(A() As Associative)
Dim i As Long
    Debug.Print "Key", "Value"
    For i = LBound(A) To UBound(A)
        Debug.Print A(i).Key, A(i).Value
    Next i
    Debug.Print "-----------------------------"
End Sub

Second way (simply) : with the Scripting Dictionary Object :

Sub Main_With_Dictionary()
Dim Base As Object, Update As Object, Merged As Object, K As Variant
    'INIT VARIABLE
    Set Base = CreateObject("Scripting.Dictionary")
    Set Update = CreateObject("Scripting.Dictionary")
    Set Merged = Base
    'FILL Base & Update
    Base.Add "name", "Rocket Skates"
    Base.Add "price", 12.75
    Base.Add "color", "yellow"
    Update.Add "price", 15.25
    Update.Add "color", "red"
    Update.Add "year", 1974
    'Fill Merge
    For Each K In Update.Keys
        Merged(K) = Update(K)
    Next
    'Print Out
    Debug.Print "Key", "Value"
    For Each K In Merged.Keys
        Debug.Print K, Merged(K)
    Next K
End Sub

And the Third : With a Class Module (named ClassArrayAssociative) The Class Module code:

Option Explicit

Private mKey As String
Private mValue As Variant

Public Property Get Value() As Variant
    Value = mValue
End Property
Public Property Let Value(iValue As Variant)
    mValue = iValue
End Property
Public Property Get Key() As Variant
    Key = mKey
End Property
Private Property Let Key(iKey As Variant)
    mKey = iKey
End Property

Public Sub Add(K As String, V As Variant)
    Value = V
    Key = K
End Sub

The Module code :

Sub Main_With_Class()
Dim Base(2) As New ClassArrayAssociative, Up(2) As New ClassArrayAssociative
    ReDim Result(UBound(Base)) As New ClassArrayAssociative
    'FILL Base & Update
    Base(0).Add "name", "Rocket Skates"
    Base(1).Add "price", 12.75
    Base(2).Add "color", "yellow"
    Result = Base
    Up(0).Add "price", 15.25
    Up(1).Add "color", "red"
    Up(2).Add "year", 1974
    'Update Result with Up
    Update Result, Up
    'Print Out
    Print_Out_2 Result
End Sub
Private Sub Update(R() As ClassArrayAssociative, U() As ClassArrayAssociative)
Dim i As Long, j As Long, Flag As Boolean
    For i = LBound(U) To UBound(U)
        j = LBound(R)
        Flag = False
        Do While j <= UBound(R) And Not Flag
            If R(j).Key = U(i).Key Then
                R(j).Value = U(i).Value
                Flag = True
            End If
            j = j + 1
        Loop
        If Not Flag Then
            ReDim Preserve R(UBound(R) + 1)
            Set R(UBound(R)) = New ClassArrayAssociative
            R(UBound(R)).Add U(i).Key, U(i).Value
        End If
    Next i
End Sub
Private Sub Print_Out_2(A() As ClassArrayAssociative)
Dim i As Long
    Debug.Print "Key", "Value"
    For i = LBound(A) To UBound(A)
        Debug.Print A(i).Key, A(i).Value
    Next i
    Debug.Print "-----------------------------"
End Sub
Output:
Key           Value
name          Rocket Skates
price          15,25 
color         red
year           1974 
-----------------------------

VBScript

set d1=createobject("Scripting.Dictionary")
d1.add "name", "Rocket Skates"
d1.add "price", 12.75
d1.add "color", "yellow"

set d2=createobject("Scripting.Dictionary")
d2.add "price", 15.25
d2.add "color", "red"
d2.add "year", 1974

set d3=createobject("Scripting.Dictionary")
for each k1 in d1.keys
   if not d3.exists(k1) then
     d3.add k1, d1(k1)
   else
     d3(k1)=d1(k1)
   end if
next	
for each k2 in d2.keys
   if not d3.exists(k2) then
      d3.add k2, d2(k2)
   else
      d3(k2)=d2(k2)
   end if
next	 

for each k3 in d3.keys
   wscript.echo k3 & vbtab & d3(k3)
next
Output:
name    Rocket Skates
price   15.25
color   red
year    1974

V (Vlang)

type Generic = int|string|f64
type Assoc = map[string]Generic

fn merge(base Assoc, update Assoc) Assoc {
    mut result := Assoc(map[string]Generic{})
    for k, v in base {
        result[k] = v
    }
    for k, v in update {
        result[k] = v
    }
    return result
}
 
fn main() {
    base := Assoc({"name": Generic("Rocket Skates"), "price": 12.75, "color": "yellow"})
    update := Assoc({"price": Generic(15.25), "color": "red", "year": 1974})
    result := merge(base, update)
    for k,v in result {
        println('$k: $v')
    }
}
Output:
name: Generic('Rocket Skates')
price: Generic(15.25)
color: Generic('red')
year: Generic(1974)

Wren

var mergeMaps = Fn.new { |m1, m2|
    var m3 = {}
    for (key in m1.keys) m3[key] = m1[key]
    for (key in m2.keys) m3[key] = m2[key]
    return m3
}     

var base = { "name": "Rocket Skates" , "price": 12.75, "color": "yellow" }
var update = { "price": 15.25, "color": "red", "year": 1974 }
var merged = mergeMaps.call(base, update)
System.print(merged)
Output:
{name: Rocket Skates, color: red, price: 15.25, year: 1974}

zkl

base:=Dictionary(
   "name", 	"Rocket Skates",
   "price", 	12.75,
   "color", 	"yellow",);
update:=Dictionary(
   "price", 	15.25,
   "color", 	"red",
   "year", 	1974,);

update.pump( new:=base.copy() );

new.pump(Void,fcn([(k,v)]){ println("%s\t%s".fmt(k,v)) });
Output:
price	15.25
color	red
year	1974
name	Rocket Skates