Jump to content

_ArrayUnique()


litlmike
 Share

Recommended Posts

This function is modified from SmOke_N's original work seen here: http://www.autoitscript.com/forum/index.ph...rt=#entry378823

The Function takes an array dimension and outputs an array that contains only the unique strings found in the elements. The Function has been modified for inclusion in the AutoIt install set and to allow usage with 2-D Arrays. The result is still a 1-D array, however, this allows the user to choose which dimension to work with. Any feedback is welcome.

#include-once
#include <Array.au3>

; #INDEX# =======================================================================================================================
; Title .........: ArrayUnique UDF for the Array.au3 library in AutoIt v3
; AutoIt Version: 3.2.3++
; Language:       English
; Description:    A function to return an Array with only unique strings
;                 Author(s) include: SmOke_N and litlmike
; ===============================================================================================================================

; #VARIABLES# ===================================================================================================================
; ===============================================================================================================================
;  Constants
; ===============================================================================================================================

; ===============================================================================================================================
;==============================================================================================================================
; #CURRENT# =====================================================================================================================
;_ArrayUnique
; ===============================================================================================================================


; #FUNCTION# ;===============================================================================
; Name...........: _ArrayUnique
; Description ...: Returns the Unique Elements of a 1-dimensional array.
; Syntax.........: Func _ArrayUnique($aArray[, $iDimension = 1[, $iBase = 0[, $iCase = 0[, $vDelim = "|"]]]])
; Parameters ....: $sArray - The Array to use
;                  $iDimension  - [optional] The Dimension of the Array to use
;                  $iBase  - [optional] Is the Array 0-base or 1-base index.  0-base by default
;                  $iCase  - [optional] Flag to indicate if the operations should be case sensitive.
;                  0 = not case sensitive, using the user's locale (default)
;                  1 = case sensitive
;                  2 = not case sensitive, using a basic/faster comparison
;                  $vDelim  - [optional] One or more characters to use as delimiters.  However, cannot forsee its usefullness
; Return values .: Success - Returns a 1-dimensional array containing containing only the unique elements of that Dimension
;                  Failure - Returns 0 and Sets @Error:
;                  0 - No error.
;                  1 - Returns 0 if parameter is not an array.
;                  2 - _ArrayUnique failed for some other reason
;                  3 - Array dimension is invalid, should be an integer greater than 0
; Author ........: SmOke_N
; Modified.......: litlmike
; Remarks .......: Returns an array, the first element ($array[0]) contains the number of strings returned, the remaining elements ($array[1], $array[2], etc.) contain the unique strings.
; Related .......: _ArrayMax, _ArrayMin
; Link ..........;
; Example .......; Yes
;
; ;==========================================================================================
Func _ArrayUnique($aArray, $iDimension = 1, $iBase = 0, $iCase = 0, $vDelim = "|")
    ;$aArray used to be ByRef, but litlmike altered it to allow for the choosing of 1 Array Dimension, without altering the original array
    If $vDelim = "|" Then $vDelim = Chr(01) ; by SmOke_N, modified by litlmike
    If Not IsArray($aArray) Then Return SetError(1, 0, 0) ;Check to see if it is valid array
    
    ;Checks that the given Dimension is Valid
    If Not $iDimension > 0 Then
        Return SetError(3, 0, 0) ;Check to see if it is valid array dimension, Should be greater than 0
    Else
        ;If Dimension Exists, then get the number of "Rows"
        $iUboundDim = UBound($aArray, 1) ;Get Number of "Rows"
        If @error Then Return SetError(3, 0, 0) ;2 = Array dimension is invalid.
        
        ;If $iDimension Exists, And the number of "Rows" is Valid:
        If $iDimension > 1 Then ;Makes sure the Array dimension desired is more than 1-dimensional
            Dim $aArrayTmp[1] ;Declare blank array, which will hold the dimension declared by user
            For $i = 0 To $iUboundDim - 1 ;Loop through "Rows"
                _ArrayAdd($aArrayTmp, $aArray[$i][$iDimension - 1]) ;$iDimension-1 to match Dimension
            Next
            _ArrayDelete($aArrayTmp, 0) ;Get rid of 1st-element which is blank
        Else ;Makes sure the Array dimension desired is 1-dimensional
            ;If Dimension Exists, And the number of "Rows" is Valid, and the Dimension desired is not > 1, then:
            ;For the Case that the array is 1-Dimensional
            If UBound($aArray, 0) = 1 Then ;Makes sure the Array is only 1-Dimensional
                Dim $aArrayTmp[1] ;Declare blank array, which will hold the dimension declared by user
                For $i = 0 To $iUboundDim - 1
                    _ArrayAdd($aArrayTmp, $aArray[$i])
                Next
                _ArrayDelete($aArrayTmp, 0) ;Get rid of 1st-element which is blank
            Else ;For the Case that the array is 2-Dimensional
                Dim $aArrayTmp[1] ;Declare blank array, which will hold the dimension declared by user
                For $i = 0 To $iUboundDim - 1
                    _ArrayAdd($aArrayTmp, $aArray[$i][$iDimension - 1]) ;$iDimension-1 to match Dimension
                Next
                _ArrayDelete($aArrayTmp, 0) ;Get rid of 1st-element which is blank
            EndIf
        EndIf
    EndIf
    
    Local $sHold ;String that holds the Unique array info
    For $iCC = $iBase To UBound($aArrayTmp) - 1 ;Loop Through array
        ;If Not the case that the element is already in $sHold, then add it
        If Not StringInStr($vDelim & $sHold, $vDelim & $aArrayTmp[$iCC] & $vDelim, $iCase) Then _
                $sHold &= $aArrayTmp[$iCC] & $vDelim
    Next
    If $sHold Then
        $aArrayTmp = StringSplit(StringTrimRight($sHold, StringLen($vDelim)), $vDelim, 1) ;Split the string into an array
        Return $aArrayTmp ;SmOke_N's version used to Return SetError(0, 0, 0)
    EndIf
    Return SetError(2, 0, 0) ;If the script gets this far, it has failed
EndFunc   ;==>_ArrayUniqueoÝ÷ Ù«­¢+Ø쨨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨(ìáµÁ±Ä´±Éĵ¥µ¹Í¥½¹°ÉÉä°Ñ¡Ð½¹Ñ¥¹ÌÕÁ±¥ÑÙ±Õ̸(ìUÍ}ÉÉåU¹¥ÅÕѼ
ÉÑ9ÜÉÉäѡн¹±ä½¹Ñ¥¹ÌÑ¡Õ¹¥ÅÕÙ±Õ̸(쨨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨(¥¹±Õ±ÐíÉÉä¹ÔÌÐì)¥´ÀÌØíÉÉålÄÁtôlİȰ̰а԰İȰ̰аÕt)}ÉÉå¥ÍÁ±ä ÀÌØíÉÉä°ÅÕ½ÐìÀÌØíÉÉäÅÕ½Ðì¤(ÀÌØí9ÝÉÉäô}ÉÉåU¹¥ÅÕ ÀÌØíÉÉä¤íUÍ¥¹Õ±ÐAɵÑÉÌ)}ÉÉå¥ÍÁ±ä ÀÌØí9ÝÉÉä°ÅÕ½ÐìÀÌØí9ÝÉÉäÉÁÉ͹ÑÌÑ¡ÅÍÐ¥µ¹Í¥½¸½ÀÌØíÉÉäÅÕ½Ðì¤((쨨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨(ìáµÁ±È´±Éȵ¥µ¹Í¥½¹°ÉÉä°Ñ¡Ð½¹Ñ¥¹ÌÕÁ±¥ÑÙ±Õ̸(ìUÍ}ÉÉåU¹¥ÅÕѼ
ÉÑ9Üĵ¥µ¹Í¥½¹°ÉÉäѡн¹±ä½¹Ñ¥¹ÌÑ¡Õ¹¥ÅÕÙ±Õ̸(쨨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨(¥¹±Õ±ÐíÉÉä¹ÔÌÐì)¥´ÀÌØíÉÉålÙulÉtômlÄ°ÅÕ½ÐíÅÕ½Ðít±lÈ°ÅÕ½ÐíÅÕ½Ðít±lÌ°ÅÕ½ÐíÅÕ½Ðít±lÄ°ÅÕ½ÐíÅÕ½Ðít±lÈ°ÅÕ½ÐíÅÕ½Ðít±lÌ°ÅÕ½ÐíÅÕ½Ðíut)}ÉÉå¥ÍÁ±ä ÀÌØíÉÉä°ÅÕ½ÐìÀÌØíÉÉäÅÕ½Ðì¤(ÀÌØí9ÝÉÉäô}ÉÉåU¹¥ÅÕ ÀÌØíÉÉä¤íUÍ¥¹Õ±ÐAɵÑÉÌ)}ÉÉå¥ÍÁ±ä ÀÌØí9ÝÉÉä°ÅÕ½ÐìÀÌØí9ÝÉÉäÉÁÉ͹ÑÌÑ¡ÅÍÐ¥µ¹Í¥½¸½ÀÌØíÉÉäÅÕ½Ðì¤((ÀÌØí9ÝÉÉäô}ÉÉåU¹¥ÅÕ ÀÌØíÉÉä°È¤íUÍ¥¹É¹¥µ¹Í¥½¸)}ÉÉå¥ÍÁ±ä ÀÌØí9ÝÉÉä°ÅÕ½ÐìÀÌØí9ÝÉÉäÉÁÉ͹ÑÌѡɹ¥µ¹Í¥½¸½ÀÌØíÉÉäÅÕ½Ðì¤((쨨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨(ìáµÁ±Ì´±Éĵ¥µ¹Í¥½¹°ÉÉä°Ñ¡Ð½¹Ñ¥¹ÌÕÁ±¥ÑÙ±Õ̸(ìUÍ}ÉÉåU¹¥ÅÕ¹Í͹ͥѥ٥ÑäѼ
ÉÑ9ÜÉÉä°Ý¥Ñ ½¹±äÑ¡Õ¹¥ÅÕÙ±Õ̸(쨨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨(¥¹±Õ±ÐíÉÉä¹ÔÌÐì)¥´ÀÌØíÉÉålÙulÉtômlÄ°ÅÕ½ÐíÅÕ½Ðít±lÈ°ÅÕ½ÐíÅÕ½Ðít±lÌ°ÅÕ½ÐíÅÕ½Ðít±lÄ°ÅÕ½ÐíÅÕ½Ðít±lÈ°ÅÕ½ÐíÅÕ½Ðít±lÌ°ÅÕ½ÐíÅÕ½Ðíut)}ÉÉå¥ÍÁ±ä ÀÌØíÉÉä°ÅÕ½ÐìÀÌØíÉÉäÅÕ½Ðì¤(ÀÌØí9ÝÉÉäô}ÉÉåU¹¥ÅÕ ÀÌØíÉÉä°Ä°À°Ä¤íUÍ¥¹Õ±ÐAɵÑÉÌ°Ý¥Ñ 
͵M¹Í¥Ñ¥Ù¥Ñä)}ÉÉå¥ÍÁ±ä ÀÌØí9ÝÉÉä°ÅÕ½ÐìÀÌØí9ÝÉÉäÉÁÉ͹ÑÌÑ¡ÅÍÐ¥µ¹Í¥½¸½ÀÌØíÉÉäÅÕ½Ðì¤((ÀÌØí9ÝÉÉäô}ÉÉåU¹¥ÅÕ ÀÌØíÉÉä°È°À°Ä¤íUÍ¥¹Õ±ÐAɵÑÉÌ°Ý¥Ñ 
͵M¹Í¥Ñ¥Ù¥Ñä)}ÉÉå¥ÍÁ±ä ÀÌØí9ÝÉÉä°ÅÕ½ÐìÀÌØí9ÝÉÉäÉÁÉ͹ÑÌÑ¡ÉÍÐ¥µ¹Í¥½¸½ÀÌØíÉÉäÅÕ½Ðì¤((쨨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨(ìáµÁ±Ð´±Éĵ¥µ¹Í¥½¹°ÉÉä°Ñ¡Ð½¹Ñ¥¹ÌÕÁ±¥ÑÙ±Õ̹ÅÕ½ÐíðÅÕ½Ðì¸(ìUÍ}ÉÉåU¹¥ÅÕѼ
ÉÑ9ÜÉÉä°Ý¥Ñ ½¹±äÑ¡Õ¹¥ÅÕÙ±Õ̸(쨨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨(¥¹±Õ±ÐíÉÉä¹ÔÌÐì)¥´ÀÌØíÉÉålÙulÉtômlÄ°ÅÕ½ÐíñÅÕ½Ðít±lÈ°ÅÕ½ÐíÅÕ½Ðít±lÌ°ÅÕ½ÐíÅÕ½Ðít±lÄ°ÅÕ½ÐíñÅÕ½Ðít±lÈ°ÅÕ½ÐíÅÕ½Ðít±lÌ°ÅÕ½ÐíÅÕ½Ðíut)1½°ÀÌØíÍ5Í  ½à((ÀÌØí9ÝÉÉäô}ÉÉåU¹¥ÅÕ ÀÌØíÉÉä°È¤íUÍ¥¹É¹µ¥µ¹Í¥½¸()½ÈÀÌØí¤ôÀQ¼ÀÌØí9ÝÉÉålÁt($ÀÌØíÍ5Í  ½àµÀìôÅÕ½ÐílÅÕ½ÐìµÀìÀÌØí¤µÀìÅÕ½ÐítèÅÕ½ÐìµÀìÀÌØí9ÝÉÉålÀÌØí¥tµÀì
I1)9áÐ()5Í   ½à À°ÅÕ½ÐíIÍÕ±ÑÌÅÕ½Ðì°ÀÌØíÍ5Í ½à¤íM¡½ÜÑ¡IÍÕ±ÑÌ((í5ÕÍС¹ÁɵÑÉÌѼ͡½Ü¸±µ¹Ð½¹Ñ¥¹¥¹ÅÕ½ÐíðÅÕ½Ð쥸}ÉÉå¥ÍÁ±ä)}ÉÉå¥ÍÁ±ä ÀÌØí9ÝÉÉä°ÅÕ½ÐìÀÌØí9ÝÉÉäÉÁÉ͹ÑÌÑ¡ÅÍÐ¥µ¹Í¥½¸½ÀÌØíÉÉäÅÕ½Ðì°´Ä°À°ÅÕ½ÐíÅÕ½Ðì
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...