Site Tools


Differences

This shows you the differences between two versions of the page.

Link to this comparison view

developer:scriptsamples:findduplicatestrings [2015/09/14] (current)
Line 1: Line 1:
 +======Finding Duplicate Strings======
 +> **Developer:​** //​[[developer:​rhinoscript|RhinoScript]]//​
 +> **Summary:​** //​Demonstrates of finding duplicate string using RhinoScript.//​
  
 +=====Question=====
 +I have an array of strings which contains duplicates. I know that RhinoScript has a method to [[http://​4.rhino3d.com/​5/​rhinoscript/​utility_methods/​cullduplicatestrings.htm|cull the duplicate strings]]. But rather than cull them, I would like to find them. I am looking for a routine that will return the indices of the duplicate items. ​
 +
 +Better yet, I would like a routine that would return sets of indices, with each set containing the indices of a particular string. For example, if my array contained "​Curve",​ Surface",​ "​curve",​ "​surface",​ I would like to have an array containing [0,2] and [1,3] returned. Any help on this would be appreciated.
 +
 +=====Answer=====
 +VBScript'​s **[[http://​msdn.microsoft.com/​en-us/​library/​x4k5wbx4(v=vs.84).aspx|Dictionary]]** is a useful tool for storing associative data, or data in the form of (key, item) pairs. In the problem outlined above, you could use a Dictionary to track each string and the indices where is appears in the array. In other words, use a Dictionary to store (string, indices) pairs. ​
 +
 +To store the indices, you are going to need an array. But creating and resizing VBScript arrays is always a challenge. So, you might consider using a .NET's **[[http://​msdn.microsoft.com/​en-us/​library/​system.collections.arraylist(v=vs.100).aspx|ArrayList]]** object. A .NET ArrayList is a COM-enabled object, which means it can be used in VBScript.
 +
 +The following sample function demonstrates how you can use a Dictionary of strings and .NET ArrayList objects to find the indices of duplicate string items in an array.
 +
 +<code vb>
 +Function FindDuplicateStrings(arrStrings,​ blnCase)
 +  ​
 +  ' Local variables
 +  Dim objDict, strKey, objItem, arrItems
 +  Dim i, j, nCount
 +  Dim arrResults()
 +
 +  ' Default return value
 +  FindDuplicateStrings = Null
 +  ​
 +  ' Create a dictionary object and set it's compare mode
 +  Set objDict = CreateObject("​Scripting.Dictionary"​) ​
 +  If (blnCase = True) Then
 +    objDict.CompareMode = vbBinaryCompare
 +  Else
 +    objDict.CompareMode = vbTextCompare
 +  End If
 +
 +  ' Process input strings. If the string is not in the dictionary,
 +  ' then add it and add it's index to the ArrayList. Otherwise,
 +  ' just add it's index to the dictionary item's existing ArrayList.
 +  For i = 0 To UBound(arrStrings)
 +    strKey = arrStrings(i)
 +    If Not objDict.Exists(strKey) Then
 +      objDict.Add strKey, CreateObject("​System.Collections.ArrayList"​)
 +    End If      ​
 +    objDict(strKey).Add(i)
 +  Next
 +  ​
 +  ' Find all of the dictionary items that have more than one index.
 +  ' Add those arrays to our result array
 +  nCount = 0
 +  arrItems = objDict.Items
 +  For Each objItem In arrItems
 +    If (objItem.Count > 1) Then
 +      ReDim Preserve arrResults(nCount) ​   ​
 +      arrResults(nCount) = objItem.ToArray()
 +      nCount = nCount + 1
 +    End If
 +  Next
 +  ​
 +  ' Done!
 +  FindDuplicateStrings = arrResults
 +  ​
 +End Function
 +</​code>​
 +
 +Here is an example of how to use the above function.
 +
 +<code vb>
 +Sub TestFindDuplicateStrings
 +
 +  Dim arrStrings, arrResults, arrItem, nItem, i
 +
 +  arrStrings = Array("​Curve"​ ,  _
 +                     "​Surface",​ _
 +                     "​Mesh", ​   _
 +                     "​Point", ​  _
 +                     "​Surface",​ _
 +                     "​Curve", ​  _
 +                     "​Curve"​)
 +
 +  arrResults = FindDuplicateStrings(arrStrings,​ False)
 +  If IsArray(arrResults) Then
 +    Call Rhino.Print("​Duplicate Sets = " & CStr(UBound(arrResults) + 1))
 +    For i = 0 To UBound(arrResults)
 +      Call Rhino.Print("​Set = " & CStr(i + 1))
 +      arrItem = arrResults(i)
 +      For Each nItem In arrItem
 +        Call Rhino.Print("​Item " & CStr(nItem) & " = " & arrStrings(nItem))
 +      Next
 +    Next
 +  End If
 +
 +End Sub
 +</​code>​
 +
 +=====More Information=====
 +[[http://​wiki.mcneel.com/​developer/​scriptsamples/​arrayutilities|Array Utilities]]\\
 +[[http://​wiki.mcneel.com/​developer/​scriptsamples/​dotnetsort|Sorting VBScript Arrays with .NET]]\\
 +[[http://​wiki.mcneel.com/​developer/​scriptsamples/​dictionaryobject|The Dictionary Object]]\\
 +[[http://​wiki.mcneel.com/​developer/​scriptsamples/​dictionary|Utilizing the Dictionary Object]]\\
 +
 +
 +{{tag>​Developer RhinoScript}}
developer/scriptsamples/findduplicatestrings.txt ยท Last modified: 2015/09/14 (external edit)