Associative array/Merging: Difference between revisions

Line 1,216:
{{output}}
<pre>["price": 15.25, "name": "Rocket Skates", "color": "red", "year": 1974]</pre>
 
=={{header|VBA}}==
3 ways to do this tasks :
First : With Arrays + Type
<lang vb>
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</lang>
 
Second way (simply) : with the Scripting Dictionary Object :
<lang vb>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</lang>
 
And the Third : With a Class Module (named ClassArrayAssociative)
The Class Module code:
<lang vb>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</lang>
The Module code :
<lang vb>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</lang>
{{out}}
<pre>
Key Value
name Rocket Skates
price 15,25
color red
year 1974
-----------------------------</pre>
 
 
 
=={{header|Wren}}==
Anonymous user