Sparx Systems Forum

Enterprise Architect => Automation Interface, Add-Ins and Tools => Topic started by: MatthiasVDE on March 10, 2017, 04:26:06 am

Title: VBScript - Remove duplicate elements from arraylist
Post by: MatthiasVDE on March 10, 2017, 04:26:06 am
Is it possible to remove duplicate elements from an arraylist?
I tried with some functions but no success.

Code: [Select]
'returns an ArrayList without duplicates
function removeDuplicates(arraylist)

dim result
set result = CreateObject("System.Collections.ArrayList")

dim element as EA.Element
for each element in arrayList
dim b
b = contains(arraylist, element)

'if contains then
'
'else
'
'end if


next
set removeDuplicates = result
end function


Code: [Select]
'returns boolean
function contains(arraylist, element)
set contains = false
dim res as EA.Element
for each res in arrayList
if res.ElementID = element.ElementID then
set contains = true
exit for
end if
next


end function
Title: Re: VBScript - Remove duplicate elements from arraylist
Post by: Geert Bellekens on March 10, 2017, 06:15:25 am
You have to iterate backwards through the arraylist and then keep a second arraylist (or dictionary) with the one that you have already encountered.
If you then have a duplicate you can remove it safely from the arraylist.

Another option is to make a new arraylist (or dictionary) object and add only the unique ones.

There are some examples of deleting elements from a list in the VBScript library (https://github.com/GeertBellekens/Enterprise-Architect-VBScript-Library)

Geert
Title: Re: VBScript - Remove duplicate elements from arraylist
Post by: MatthiasVDE on March 10, 2017, 06:13:48 pm
I found it:

Code: [Select]
'returns an ArrayList without duplicates
function removeDuplicates(arraylist)
dim result
set result = CreateObject("System.Collections.ArrayList")
dim element as EA.Element
for each element in arrayList
dim b
b = contains(result, element)
if b = false then
result.add(element)
end if
next
set removeDuplicates = result
end function


'returns boolean
function contains(result, element)
contains = false
dim res as EA.Element
for each res in result
if res.ElementID = element.ElementID then
contains = true
exit for
end if
next
end function