Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


Compare two arrays and store matching items into new array

 
Logged in as: Guest
arrSession:exec spGetSession 2,2,40686
 Active Users: There are 0 members and 0 guests.
 Users viewing this topic: none
 

 

 
  
  Printable Version
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Compare two arrays and store matching items into new array
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1]
Login
Message << Older Topic   Newer Topic >>
 Compare two arrays and store matching items into new array - 12/10/2006 11:47:09 AM   
  mrmore

 

Posts: 5
Score: 0
Joined: 11/9/2006
Status: offline
Ive used this function to process large amounts of data. Hopefully someone else will also find it useful.
Compares array1 to array2 and the results are assigned to the function.




Option Explicit
Dim array1, array2, arrmatch, item
array1 = Array("item1", "item2", "item3", "item4", "item5", "item6", "item7", "item8", "item9", "item10")
array2 = Array("item4", "item5", "item6", "item7", "item8", "item9", "item3", "item11", "item12", "item13")
arrMatch = CompArrays(array1, array2)
If IsArray(arrMatch) then
For each item in arrMatch
MsgBox item
Next
Else
MsgBox arrMatch
End If




Private Function CompArrays(ByVal array1, array2)
Dim intMatching' as Integer
Dim arrItem1, arrItem2, arrMatch, i
Dim bolMatch' as Boolean
Dim strMatch' as String
intMatching = 0
If IsArray(array1) AND IsArray(array2) Then
' Count matching items to define array
For each arrItem1 in array1
bolMatch = 0
For each arrItem2 in array2
 If UCase(arrItem1) = UCase(arrItem2) Then
  bolMatch = 1
  Exit For
 End If
Next
If bolMatch = 1 Then
 intMatching = intMatching + 1
End If
Next
If intMatching > 0 Then
ReDim arrMatch(intMatching - 1)
For each arrItem1 in array1
 bolMatch = 0
 For Each arrItem2 in array2
  If UCase(arrItem1) = UCase(arrItem2) Then
   bolMatch = 1
   Exit For
  End If
 Next
 If bolMatch = 1 Then
  For i = 0 to intMatching
   If VarType(arrMatch(i)) = 0 Then
    arrMatch(i) = arrItem1
    Exit For
   End If
  Next
 End If
Next
Else
strMatch = "Failed: Matching items not found"
End If
Else
strMatch = "Failed: Input not an array"
End If
If InStr(1, strMatch, "Failed", 1) Then
CompArrays = strMatch
Else
CompArrays = arrMatch
End If
End Function

< Message edited by mrmore -- 12/10/2006 2:10:06 PM >
 
 
Post #: 1
 
 RE: Compare two arrays and store matching items into ne... - 12/10/2006 9:01:07 PM   
  ehvbs

 

Posts: 2172
Score: 50
Joined: 6/22/2005
From: Germany
Status: online
Hi mrmore,

seeing your interest in "compare arrays", I'm wondering if you feel like
looking at

   http://www.visualbasicscript.com/m_40495/tm.htm

where we (ashok_ganeshs, ehvbs, ebgreen, ...) face this very problem.
To spike your interest:


      

(in reply to mrmore)
 
 
Post #: 2
 
 
 
  

If you found our site useful please link to us <a href="http://www.visualbasicscript.com">VisualBasicScript.com</a>.
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Compare two arrays and store matching items into new array Page: [1]
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts