Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 448
  • Last Modified:

I need help comparing two arrays in VB.Net 3.5

Hi Experts,
Hi Experts,
I have a VB.Net application where I need to compare the values of two arrays.  If a match is found it needs to update the status column/dimmension in both arrays to Match.  Say I have the following two arrays:

oldArray(2,1) and newArray(2,1) with the following values:

oldArray(0,0) = Amy Rogers
oldArray(0,1) = Old
oldArray(1,0) = Sean Adams
oldArray(1,1) = Old
oldArray(2,0) = Richard Smith
oldArray(2,1) = Old

AND

newArray(0,0) = Robert Berger
newArray(0,1) = New
newArray(1,0) = Sean Adams
newArray(1,1) = New
newArray(2,0) = Tracey Schultz
newArray(2,1) = New

After the Arrays are compared the arrays will look like this, notice that the status in both arrays has been updated to Match for Sean Adams:

oldArray(0,0) = Amy Rogers
oldArray(0,1) = Old
oldArray(1,0) = Sean Adams
oldArray(1,1) = Match
oldArray(2,0) = Richard Smith
oldArray(2,1) = Old

AND

newArray(0,0) = Robert Berger
newArray(0,1) = New
newArray(1,0) = Sean Adams
newArray(1,1) = Match
newArray(2,0) = Tracey Schultz
newArray(2,1) = New


How can I do this VB.Net?

Thanks in advance,
mrotor
0
mainrotor
Asked:
mainrotor
  • 3
3 Solutions
 
nepaluzCommented:
I am not very likeable to arrays (and multidimensionals even worse!) but can suggesta solution using a dictionary (which is generally the same thing, I think ...)
Here we go.

If you only require to update the new array, this will work wonders for you
        Dim OldARR As New Dictionary(Of String, String) From {{"Amy Rogers", "OLD"}, {"Sean Adams", "OLD"}, {"Richard Smith", "OLD"}}
        Dim NEWARR As New Dictionary(Of String, String) From {{"Robert Berger", "NEW"}, {"Sean Adams", "NEW"}, {"Tracey Schultz", "NEW"}}
        Dim Common = OldARR.Keys.Intersect(NEWARR.Keys)
        If Common.Count > 0 Then
            For Each x In Common
                If NEWARR.Keys.Contains(x) Then NEWARR.Item(x) = "Match"
            Next
        End If

Open in new window

PS. It is .NET4.0 code to intialise the dictionaries, but you can apply .NET3.5 syntax and the result will be the same.
0
 
käµfm³d 👽Commented:
Here's my offering:
Sub MarkMatches(ByRef oldArray(,) As String, ByRef newArray(,) As String)
    For i As Integer = 0 To oldArray.Length - 1
        If oldArray(i, 0) = newArray(i, 0) Then
            oldArray(i, 1) = "Match"
        End If
    Next
End Sub

Open in new window

0
 
käµfm³d 👽Commented:
Disregard the above.
0
 
käµfm³d 👽Commented:
This is what the above should have been:
Sub MarkMatches(ByRef oldArray(,) As String, ByRef newArray(,) As String)
    For i As Integer = 0 To oldArray.GetUpperBound(0) - 1
        If oldArray(i, 0) = newArray(i, 0) Then
            oldArray(i, 1) = "Match"
        End If
    Next
End Sub

Open in new window

0
 
Jeff CertainCommented:
I'd add an extra check to what kaufmed proposed. If oldArray.Length <> newArray.Length then you know the arrays can't possibly match.
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now