Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

vb 2008 - Comparing Array elements

Posted on 2009-05-07
4
Medium Priority
?
633 Views
Last Modified: 2012-05-06
Is there any way in VB 2008 to compare two arrays to find elements missing from arrary1 in array2?
0
Comment
Question by:hdgh
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 18

Expert Comment

by:carlnorrbom
ID: 24326401
Hi,

You can iterate over Array2 and compare the current element in Array2 with the corresponding element in Array1 but that would require both arrays having the same upper bound, i.e. all elements have either a value or null.

/Carl.
0
 
LVL 18

Expert Comment

by:carlnorrbom
ID: 24326469
Hi,

Just realized a bit of code could be handy, sorry.

/Carl.
        Dim Array1() As String = {"Value1", "Value2", "Value3", "Value4"}
        Dim Array2() As String = {"Value1", "Value2", "", "Value4"}
        Dim i As Integer = 0
        Do While i <= UBound(Array2)
            If Not String.Compare(Array2(i).ToString, Array1(i).ToString) = 0 Then
                Throw New Exception("Array Element " & i.ToString & " in Array1 is missing or not the same as in Array2")
            End If
            i += 1
        Loop

Open in new window

0
 

Author Comment

by:hdgh
ID: 24327098
Thanks for your reply.

the arrays may be of different size ( ary1 = 200 ele ; ary2 300 ele).  I need to determine if the values of  ary1 are missing from ary2.

I have the following....my logic must be wrong....another set of eyes would help !! ( see code snippet)


    Dim ary1() As String = {"123456789", "123456787", "123456788", "185456478", "184576781", "999199912"}
    Dim ary2() As String = {"123456789", "999123124", "123499541", "123456787", "123456788", "185456478", "184576781"}
    Dim bFound, bNoDiff As Boolean
    bNoDiff = True
 
    Array.Sort(ary1)
    Array.Sort(ary2)
    bNoDiff = True
 
    For intX = 0 To UBound(ary1)
      bFound = False
      For intY = 0 To UBound(ary2)
        If Array.BinarySearch(ary1, ary2(intY).ToString) Then
          bFound = True
          Exit For
        End If
      Next
      If bFound = False Then
        bNoDiff = False
        MsgBox("Missing: " & ary1(intX))
      End If
    Next
 
    If bNoDiff Then
      MsgBox("0 Differences Found")
    End If

Open in new window

0
 

Accepted Solution

by:
hdgh earned 0 total points
ID: 24328187
I figured it out ! Unfortunatly, it's quite slow.

I replaced the if statement in line 13 (code from previous post) to:
If ary1(intX).ToString = ary2(intY).ToString Then

It works...however It has to compare every element in ary1 to every element in ary2....NOT EFFICIENT !!

anybody have any other ideas ?

Thanks !
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question