Sign in to follow this  
Followers 0
BugFix

Sort 1D/2D -Array and delete doubles

1 post in this topic

#1 ·  Posted (edited)

Hi,

by extracting data from files or from web-sites it may be, that some entries are double (i.e. user name).

With this function i sort an array and delete doubles in it. With string i can choose case sensitivity.

It works with 2D Array only with 2 occurences in the second dimension. (If you need more -you can expand this script :shocked: )

Here with an example:

CODE
#include <array.au3>
Dim $ar[10]
Dim $ar2D[10][2]

$ar[0] = "Otto"
$ar[1] = "otto"
$ar[2] = "Bernd"
$ar[3] = "Kurt"
$ar[4] = "otto"
$ar[5] = "Kurt"
$ar[6] = "Bernd"
$ar[7] = "Otto"
$ar[8] = "Kurt"
$ar[9] = "bernd"

$ar2 = $ar

$ar2D[0][0] = "Otto"
$ar2D[1][0] = "Otto"
$ar2D[2][0] = "Bernd"
$ar2D[3][0] = "Kurt"
$ar2D[4][0] = "otto"
$ar2D[5][0] = "Kurt"
$ar2D[6][0] = "Bernd"
$ar2D[7][0] = "Otto"
$ar2D[8][0] = "Kurt"
$ar2D[9][0] = "bernd"

$ar2D[0][1] = "meier"
$ar2D[1][1] = "meier"
$ar2D[2][1] = "McBee"
$ar2D[3][1] = "Becker"
$ar2D[4][1] = "Meier"
$ar2D[5][1] = "Schramm"
$ar2D[6][1] = "McBee"
$ar2D[7][1] = "Lorenz"
$ar2D[8][1] = "meier"
$ar2D[9][1] = "Gantzer"

$ar2D2 = $ar2D

_ArrayDisplay($ar, "unsorted 1D")
_ArraySortDblDel($ar)
_ArrayDisplay($ar, "w/o case sens 1D")
_ArraySortDblDel($ar2,1)
_ArrayDisplay($ar2, "case sens 1D")
_ArrayDisplay2D($ar2D, "unsorted 2D")
_ArraySortDblDel($ar2D,0,0,2,1)
_ArrayDisplay2D($ar2D, "w/o case sens 2D")
_ArraySortDblDel($ar2D2,1,0,2,1)
_ArrayDisplay2D($ar2D2, "case sens 2D")


;----------------------------------------------------------------------------------------------------------------------
; Function      _ArraySortDblDel(ByRef $ARRAY [, $CASESENS=0 [, $iDESCENDING=0 [, $iDIM=0[ , $iSORT=0]]]])
;
; Description   - Sorts an 1D/2D Array and delete double entries (2D -> combination by '[n][0]' and '[n][1]').
;               - By using string, you can choose case sensitivity.
;               - BaseIndex is 0; sorts the whole array.
;
; Parameter     $ARRAY:         Array to sort
;   optional    $CASESENS:      Case sensitivity off[0] or on[1] (default 0)
;   optional    $iDESCENDING:   Sort ascending[0]/descending[1] (default 0)
;   optional    $iDIM:          Occurences in the second dimension, eg $A[100]=0 or $A[100][2]=2 (default 0)        
;   optional    $iSORT:         SortIndex by using 2 dimensions (default 0)
;
; Return        ByRef sorted Array without doubles
;
; Requirements  Only 2 occurences in the second dimension
;               #include <array.au3>
;
; Author        BugFix (bugfix@autoit.de)
;----------------------------------------------------------------------------------------------------------------------

Func _ArraySortDblDel(ByRef $ARRAY, $CASESENS=0, $iDESCENDING=0, $iDIM=0, $iSORT=0)
Local $arTmp1D[1], $arTmp2D[1][2], $dbl = 0
    $arTmp1D[0] = ""
    $arTmp2D[0][0] = ""
    If $iDIM = 0 Then $iDIM = 1
    _ArraySort($ARRAY,$iDESCENDING,0,0,$iDIM,$iSORT)
    Switch $iDIM
        Case 1 ; 1D
            For $i = 0 To UBound($ARRAY)-1
                $dbl = 0
                For $k = 0 To UBound($arTmp1D)-1
                    Switch $CASESENS
                        Case 0
                            If $arTmp1D[$k] = $ARRAY[$i] Then $dbl = 1
                        Case 1
                            If $arTmp1D[$k] == $ARRAY[$i] Then $dbl = 1
                    EndSwitch   
                Next
                If $dbl = 0 Then
                    If $arTmp1D[0] = "" Then
                        $arTmp1D[0] = $ARRAY[$i]
                    Else
                        _ArrayAdd($arTmp1D, $ARRAY[$i])
                    EndIf
                Else
                    $dbl = 0
                EndIf
            Next
            $ARRAY = $arTmp1D
        Case 2 ; 2D
            For $i = 0 To UBound($ARRAY)-1
                $dbl = 0
                For $k = 0 To UBound($arTmp2D)-1
                    Switch $CASESENS
                        Case 0
                            If  ( $arTmp2D[$k][0] = $ARRAY[$i][0] ) And _
                                ( $arTmp2D[$k][1] = $ARRAY[$i][1] ) Then $dbl = 1
                        Case 1      
                            If  ( $arTmp2D[$k][0] == $ARRAY[$i][0] ) And _
                                ( $arTmp2D[$k][1] == $ARRAY[$i][1] ) Then $dbl = 1
                    EndSwitch               
                Next
                If $dbl = 0 Then
                    If $arTmp2D[0][0] = "" Then
                        $arTmp2D[0][0] = $ARRAY[$i][0]
                        $arTmp2D[0][1] = $ARRAY[$i][1]
                    Else
                        ReDim $arTmp2D[UBound($arTmp2D)+1][2]
                        $arTmp2D[UBound($arTmp2D)-1][0] = $ARRAY[$i][0]
                        $arTmp2D[UBound($arTmp2D)-1][1] = $ARRAY[$i][1]
                    EndIf
                Else
                    $dbl = 0
                EndIf
            Next
            $ARRAY = $arTmp2D
    EndSwitch
EndFunc ; ==>_ArraySortDblDel


Func _ArrayDisplay2D($Array, $Title="")
Local $str = ""
    For $i = 0 To UBound($Array)-1
        If $i = 0 Then
            $str &= "[" & $i & "]   = " & $Array[$i][0] & " | " & $Array[$i][1]
        Else
            $str &= @LF & "[" & $i & "]   = " & $Array[$i][0] & " | " & $Array[$i][1]
        EndIf
    Next
    MsgBox(0, $Title, $str)
EndFunc

_ArraySortDblDel.au3

Edited by BugFix

Best Regards BugFix  

Share this post


Link to post
Share on other sites



Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0