Associative array/Merging: Difference between revisions
(Add Factor) |
m (→{{header|Python}}: added zkl header) |
||
Line 139: | Line 139: | ||
{{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|zkl}}== |
|||
<lang zkl></lang> |
|||
<lang zkl></lang> |
|||
{{out}} |
|||
<pre> |
|||
</pre> |
Revision as of 00:38, 14 January 2020
- 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 |
Factor
The assoc-union
word does this. assoc-union!
is a variant that mutates the first associative array.
<lang factor>USING: assocs prettyprint ;
{ { "name" "Rocket Skates" } { "price" 12.75 } { "color" "yellow" } } { { "price" 15.25 } { "color" "red" } { "year" 1974 } } assoc-union .</lang>
- Output:
V{ { "name" "Rocket Skates" } { "price" 15.25 } { "color" "red" } { "year" 1974 } }
Go
<lang 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)
}</lang>
- Output:
map[color:red name:Rocket Skates price:15.25 year:1974]
Lua
<lang 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</lang>
- Output:
price: 15.25 color: red year: 1974 name: Rocket Skates
MiniScript
MiniScript supports merging maps with the + operator. <lang MiniScript>base = {"name":"Rocket Skates", "price":12.75, "color":"yellow"} update = {"price":15.25, "color":"red", "year":1974}
result = base + update
print result</lang>
- Output:
{"color": "red", "name": "Rocket Skates", "price": 15.25, "year": 1974}
Python
As of Python 3.5, this can be solved with the dictionary unpacking operator. <lang Python>base = {"name":"Rocket Skates", "price":12.75, "color":"yellow"} update = {"price":15.25, "color":"red", "year":1974}
result = {**base, **update}
print(result)</lang>
- Output:
{'name': 'Rocket Skates', 'price': 15.25, 'color': 'red', 'year': 1974}
zkl
<lang zkl></lang> <lang zkl></lang>
- Output: