10-12-2014, 11:57 PM
I saw that this one only worked for integer array (or a string array of integers!), so I made these functions which work for either str or int array and returns total unique elements
(I would have combined into one function by passing the array (either int or str) as safearray and then testing with GetArrayType from http://www.quickmacros.com/forum/showthr...0#pid25350 but I couldn't figure out once I passed the array safely and tested it, how to convert it back into ARRAY(str) or ARRAY(int).
Anyway hope this is useful,
S
Macro GetUniqueArrayElementsExample
Function GetUniqueArrayElements_Int
Function GetUniqueArrayElements_Str
(I would have combined into one function by passing the array (either int or str) as safearray and then testing with GetArrayType from http://www.quickmacros.com/forum/showthr...0#pid25350 but I couldn't figure out once I passed the array safely and tested it, how to convert it back into ARRAY(str) or ARRAY(int).
Anyway hope this is useful,
S
Macro GetUniqueArrayElementsExample
out
ARRAY(int) a
a[] = 1
a[] = 2
a[] = 2
a[] = 3
a[] = 3
a[] = 3
a[] = 4
a[] = 5
a[] = 5
ARRAY(str) s = "red[]red[]blue[]blue[]green[]yellow[]yellow"
_i = GetUniqueArrayElements_Int(a)
out "[]"
out F"Number of unique integer elements = {_i}"
out "[]----------[]"
_i = GetUniqueArrayElements_Str(s)
out "[]"
out F"Number of unique str elements = {_i}"
Function GetUniqueArrayElements_Int
function ARRAY(int)&arrToUnDupe
int i j
ARRAY(int) b
for i 0 arrToUnDupe.len
,for(j b.len-1 -1 -1) if(b[j]=arrToUnDupe[i]) break
,if(j<0) b[]=arrToUnDupe[i]
out "array arrToUnDupe:"
for(i 0 arrToUnDupe.len) out arrToUnDupe[i]
out ""
out "array arrToUnDupe_unique:"
for(i 0 b.len) out b[i]
ret b.len
Function GetUniqueArrayElements_Str
function ARRAY(str)&arrToUnDupe
int i j
ARRAY(str) b
;create array b from unique elements of array a
for i 0 arrToUnDupe.len
,for(j b.len-1 -1 -1) if(b[j]=arrToUnDupe[i]) break
,if(j<0) b[]=arrToUnDupe[i]
out "array arrToUnDupe:"
for(i 0 arrToUnDupe.len) out arrToUnDupe[i]
out ""
out "array arrToUnDupe_unique:"
for(i 0 b.len) out b[i]
ret b.len