Jump to content

_ArrayCount


exodius
 Share

Recommended Posts

Something I put together a while back and thought I'd share.

; #FUNCTION# ;===============================================================================
;
; Name...........: _ArrayCount
; Description ...: Takes the given array, and returns an array with a count of each unique element
; Syntax.........: _ArrayCount($avArray, $vReturn)
; Parameters ....:  $avArray - The 1D array containing the elements you wish to count
;                   $vReturn - Based on the given return, get a different return
;                  |0 - Returns a 2D array where [$iNum][0] is the Element and [$iNum][1] is the count (Default)
;                  |1 - Returns a 2D array where [$iNum][0] is the count and [$iNum][1] is the Element
;                  |2 - Returns a 2D array where [$iNum][0] is the count of the count of the element that occurred the most and [$iNum][1] is the Element
;                  |3 - Returns a 2D array where [$iNum][0] is the count of the count of the element that occurred the least and [$iNum][1] is the Element
; Return values .: Success - Returns a 2D array where the output is as specified in the Parameter section above.
;                  Failure - "", sets @error:
;                  |1 - $avArray is not an array
;                  |2 - $avArray is not an 1 dimensional array
;                  |3 - $vReturn was a number other than 0-3
; Author ........: Jason Brand <exodius at gmail dot com>
; Modified.......:
; Remarks .......: If more than one element matches the criteria when using Modes 2 and 3 for the $vReturn parameter then all of the matching elements will be returned (see the example).
; Related .......: _ArrayDisplay, _ArrayDelete, _ArrayFindAll, _ArraySwap, StringSplit
; Link ..........;
; Example .......; Yes
;
; ;==========================================================================================

Func _ArrayCount($avArray, $vReturn = 0)
    If (Not IsArray($avArray)) Then Return SetError(1, 0, "")
    If UBound($avArray, 0) <> 1 Then Return SetError(2, 0, "")
    If $vReturn < 0 Or $vReturn > 3 Then Return SetError(3, 0, "")
    
    Local $avCompile[1][2], $iTotalCount, $avIndices, $avDuplicateCheck

    $iTotalCount = UBound($avArray)
    For $iNum = 0 To UBound($avArray) - 1
        $iNum = 0
        $avCompile[UBound($avCompile) - 1][0] = $avArray[$iNum]
        $avIndices = _ArrayFindAll($avArray, $avArray[$iNum])
        $avCompile[UBound($avCompile) - 1][1] = UBound($avIndices)
        
        For $iNum2 = 0 To UBound($avIndices) - 1
            _ArrayDelete($avArray, $avIndices[$iNum2] - $iNum2)
            $iTotalCount -= 1
        Next
        
        If $iNum + 1 > $iTotalCount Then ExitLoop
        ReDim $avCompile[UBound($avCompile) + 1][2]
    Next
    
    If $vReturn = 0 Then
        Return $avCompile
    ElseIf $vReturn = 1 Then
        For $iNum = 0 To UBound($avCompile) - 1
            _ArraySwap($avCompile[$iNum][1], $avCompile[$iNum][0])
        Next
        Return $avCompile
    ElseIf $vReturn = 2 Then
        Local $avResult[1][2] = [[$avCompile[0][1], $avCompile[0][0]]]
        For $iNum = 0 To UBound($avCompile) - 1
            If $iNum + 1 > UBound($avCompile) - 1 Then ExitLoop
            If $avResult[0][0] < $avCompile[$iNum + 1][1] Then
                $avResult[0][0] = $avCompile[$iNum + 1][1]
                $avResult[0][1] = $avCompile[$iNum + 1][0]
            EndIf
        Next
        
        $avDuplicateCheck = _ArrayFindAll($avCompile, $avResult[0][0], 0, 0, 0, 0, 1)
        ReDim $avResult[UBound($avDuplicateCheck)][2]
        For $iNum = 0 To UBound($avDuplicateCheck) - 1
            $avResult[$iNum][1] = $avCompile[$avDuplicateCheck[$iNum]][0]
            $avResult[$iNum][0] = $avCompile[$avDuplicateCheck[$iNum]][1]
        Next
        
        Return $avResult
    ElseIf $vReturn = 3 Then
        Local $avResult[1][2] = [[$avCompile[0][1], $avCompile[0][0]]]
        For $iNum = 0 To UBound($avCompile) - 1
            If $iNum + 1 > UBound($avCompile) - 1 Then ExitLoop
            If $avResult[0][0] > $avCompile[$iNum + 1][1] Then
                $avResult[0][0] = $avCompile[$iNum + 1][1]
                $avResult[0][1] = $avCompile[$iNum + 1][0]
            EndIf
        Next
        
        $avDuplicateCheck = _ArrayFindAll($avCompile, $avResult[0][0], 0, 0, 0, 0, 1)
        ReDim $avResult[UBound($avDuplicateCheck)][2]
        For $iNum = 0 To UBound($avDuplicateCheck) - 1
            $avResult[$iNum][1] = $avCompile[$avDuplicateCheck[$iNum]][0]
            $avResult[$iNum][0] = $avCompile[$avDuplicateCheck[$iNum]][1]
        Next
        
        Return $avResult
    EndIf
EndFunc   ;==>_ArrayCount
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...