RosettaCodeData/Task/Associative-array-Merging/FreeBASIC/associative-array-merging.b...

48 lines
1.2 KiB
Plaintext

Type Dictionary
As String key
As String value
End Type
Sub merge(original() As Dictionary, update() As Dictionary, result() As Dictionary)
Dim As Integer i, j, index
Dim As Boolean found
For i = 0 To Ubound(update)
result(i) = update(i)
Next i
index = i
For i = 0 To Ubound(original)
found = False
For j = 0 To Ubound(update)
If original(i).key = update(j).key Then
found = True
Exit For
End If
Next j
If Not found Then
result(index) = original(i)
index += 1
End If
Next i
End Sub
Dim As Dictionary original(2)
original(0).key = "name": original(0).value = "Rocket Skates"
original(1).key = "price": original(1).value = "12.75"
original(2).key = "color": original(2).value = "yellow"
Dim As Dictionary update(2)
update(0).key = "price": update(0).value = "15.25"
update(1).key = "color": update(1).value = "red"
update(2).key = "year": update(2).value = "1974"
Dim As Dictionary merged(Ubound(update) + Ubound(original) - 1)
merge(original(), update(), merged())
For i As Integer = 0 To Ubound(merged)
Print "key: "; merged(i).key; ", value: "; merged(i).value
Next i
Sleep