#163799 - 2006-06-28 05:47 PM
Removing duplicate entries
|
GSUK
Starting to like KiXtart
Registered: 2004-03-10
Posts: 125
|
Hello again!
Last post of the day - honest! 
I have a single dimension array that may contain duplicate data. For Example:
10 11 13 10 10 15 13
I need to display every unique entry so in this example the output would read:
10 11 13 15
I'm a bit stuck on how to do this 
Any advice would be most appreciated.
Thanks.
|
|
Top
|
|
|
|
#163800 - 2006-06-28 08:24 PM
Re: Removing duplicate entries
|
Howard Bullock
KiX Supporter
   
Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
|
I recently addressed this issue while working a similar problem for someone. See this thread. I have code posted and later script output posted. You will need to adjust the code slightly to output all unique values and not just the five lowest.
http://www.kixtart.org/ubbthreads/showfl...3&fpart=all
|
|
Top
|
|
|
|
#163803 - 2006-06-29 11:07 AM
Re: Removing duplicate entries
|
GSUK
Starting to like KiXtart
Registered: 2004-03-10
Posts: 125
|
Thanks for your help guys.
I'm using the UDF but there seems to be a bug in it. If you run it with the following array data it returns: ERROR : array reference out of bounds!
Code:
$array = '1233','3422','2453','3534','4343','4333','337','337','3437','3331','2444','3444','4444','4443','4433','4447','4447','4447'
$newArray = RemoveDups($array) For Each $item In $newArray ? $item Next
Function RemoveDups($ArrayToCheck) Dim $tmpArray[10], $elem, $h, $i, $j, $incr If VarType($ArrayToCheck) & 8192 $found = 0 $j = -1 $incr = 10 Else Exit 87 EndIf For $h = 0 to Ubound($ArrayToCheck) $elem = Trim($ArrayToCheck[$h]) For $i = 0 to Ubound($ArrayToCheck) If $elem <> "xxDUPxx" AND $elem <> "" AND $elem = Trim($ArrayToCheck[$i]) $found = $found+1 If $found > 1 $ArrayToCheck[$i]="xxDUPxx" EndIf EndIf Next $found=0 Next For Each $item In $ArrayToCheck If Trim($item) > '' AND $item <> "xxDUPxx" $j=$j+1 $tmpArray[$j]=Trim($item) If $j = Ubound($tmpArray) ReDim Preserve $tmpArray[Ubound($tempArray)+$incr] EndIf EndIf Next If $j = -1 Exit 1 Else ReDim Preserve $tmpArray[$j] $RemoveDups = $tmpArray EndIf EndFunction
|
|
Top
|
|
|
|
#163804 - 2006-06-29 11:40 AM
Re: Removing duplicate entries
|
Richard H.
Administrator
   
Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
|
Here is another method: Code:
Break ON $=SetOption("Explicit","ON") Dim $array,$vElement $array = '1233','3422','2453','3534','4343','4333','337','337','3437','3331','2444','3444','4444','4443','4433','4447','4447','4447' $array=udfRemoveDups($array) For Each $vElement in $array " > "+$vElement+@CRLF Next Exit 0 Function udfRemoveDups($avArray) Dim $sDelimiter $sDelimiter=@CRLF+"udfRemoveDups" Dim $iIndex For $iIndex=0 To UBound($avArray) If AScan($avArray,$avArray[$iIndex])<>$iIndex $avArray[$iIndex]="" EndIf Next $avArray=Join($avArray,$sDelimiter) While InStr($avArray,$sDelimiter+$sDelimiter) $avArray=Join(Split($avArray,$sDelimiter+$sDelimiter),$sDelimiter) Loop $udfRemoveDups=$avArray $avArray=Split($avArray,$sDelimiter) If $avArray[0]="" $udfRemoveDups=$sDelimiter+$udfRemoveDups EndIf If $avArray[UBound($avArray)]="" $udfRemoveDups=$udfRemoveDups+$sDelimiter EndIf $udfRemoveDups=Split(Join(Split($udfRemoveDups,$sDelimiter+$sDelimiter),""),$sDelimiter) EndFunction
|
|
Top
|
|
|
|
#163805 - 2006-06-29 11:46 AM
Re: Removing duplicate entries
|
Richard H.
Administrator
   
Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
|
Actually that is waaaay to complex.
This will do just as well, so long as you are not going to fall foul of string limits: Code:
Break ON $=SetOption("Explicit","ON") Dim $array,$vElement $array = '1233','3422','2453','3534','4343','4333','337','337','3437','3331','2444','3444','4444','4443','4433','4447','4447','4447' $array=udfRemoveDups($array) For Each $vElement in $array " > "+$vElement+@CRLF Next Exit 0 Function udfRemoveDups($avArray) Dim $sDelimiter $sDelimiter=@CRLF+"udfRemoveDups" Dim $iIndex For $iIndex=0 To UBound($avArray) If AScan($avArray,$avArray[$iIndex])=$iIndex $udfRemoveDups=$udfRemoveDups+$sDelimiter+$avArray[$iIndex] EndIf Next $udfRemoveDups=Split(SubStr($udfRemoveDups,Len($sDelimiter)+1),$sDelimiter) EndFunction
|
|
Top
|
|
|
|
#163806 - 2006-06-29 12:13 PM
Re: Removing duplicate entries
|
GSUK
Starting to like KiXtart
Registered: 2004-03-10
Posts: 125
|
Excellent! That works a treat.
Thanks for your help Richard.
|
|
Top
|
|
|
|
Moderator: Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart
|
0 registered
and 2220 anonymous users online.
|
|
|