Jump to content

Get number of occurrences in Array


 Share

Recommended Posts

how many items will you need to manage in real production?

... here a bit shortened version (it also allows to limit the nr. of items to scan)

#include <Array.au3>
Local $TotalRows, $aResult, $group
Local $Limit = 4; how many occurrences you want to consider (4 or less)
Local $Limit2 = 4 ; how many instances you want to print (4 or less (no more than 4)

Local $aArray[12][2] = [["Computer 1", "192.168.1.1"], _
        ["Computer 2", "192.168.1.2"], _
        ["Computer 1", "192.168.1.3"], _
        ["Computer 3", "192.168.1.4"], _
        ["Computer 3", "192.168.1.5"], _
        ["Computer 1", "192.168.1.6"], _
        ["Computer 1", "192.168.1.7"], _
        ["Computer 4", "192.168.1.8"], _
        ["Computer 5", "192.168.1.9"], _
        ["Computer 6", "192.168.1.10"], _
        ["Computer 7", "192.168.1.11"], _
        ["Computer 1", "192.168.1.12"]]

$TotalRows = UBound($aArray, $UBOUND_ROWS) - 1 ;Get total rows in the Array to search
_ArraySort($aArray, 0, 0, 0, 0) ;Sort the Array
For $iNdex = 0 To $TotalRows
    $aResult = UBound(_ArrayFindAll($aArray, $aArray[$iNdex][0])) - 1 ; occurences in this group
    For $i = $iNdex To $iNdex + $aResult ; traverse group
        If $i - $iNdex = $Limit Then ExitLoop ; limit loop to $Limit
        ConsoleWrite($aArray[$i][0] & ", " & $aArray[$i][1] & @CRLF)
    Next
    ConsoleWrite(@CRLF) ; separate each group
    $group += 1
    If $group = $Limit2 Then ExitLoop ; limit the nr of items to print at your choice
    $iNdex += $aResult ; jump to end of group
Next ; next group

 

Edited by Chimp

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Link to comment
Share on other sites

You are taking a pretty heavy time penalty by using arrayfindall, even for the additional criteria I would use Kylomas' model (and my solution is similar in speed, but I think has more edge cases)

And I think you could shave some more ms by doing your array sort on the returned array, since it will be smaller and still have the same effect.

 

*dropped it from .0006 to .0002

Local $aArray[12][2] = [["Computer 1", "192.168.1.1"], _
        ["Computer 2", "192.168.1.2"], _
        ["Computer 1", "192.168.1.3"], _
        ["Computer 3", "192.168.1.4"], _
        ["Computer 3", "192.168.1.5"], _
        ["Computer 1", "192.168.1.6"], _
        ["Computer 1", "192.168.1.7"], _
        ["Computer 3", "192.168.1.8"], _
        ["Computer 1", "192.168.1.9"], _
        ["Computer 3", "192.168.1.10"], _
        ["Computer 3", "192.168.1.11"], _
        ["Computer 1", "192.168.1.12"]]


local $sOut = ""
local $sTest = ""

$TIMER = timerinit()

For $i = 0 to ubound($aArray) - 1
    $sOut &= $aArray[$i][0] & "="
    $sOut &= $aArray[$i][1] & ","
Next

for $i = 0 to ubound($aArray) - 1

If stringinstr($sTest , $aArray[$i][0] & "::") Then continueloop
$sOut = stringregexpreplace($sOut , $aArray[$i][0] , "REPLACE" , 4)
$sOut = stringregexpreplace($sOut , $aArray[$i][0] & "=.*?," , "")
$sOut = stringregexpreplace($sOut , "REPLACE" , $aArray[$i][0])
$sTest &= $aArray[$i][0] & "::"

next

$diff = TimerDiff($TIMER) / 1000
ConsoleWrite($diff)

msgbox(0, '' , stringreplace($sOut , "," , @LF))

 

Edited by boththose
grammar

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

FYI - I have the latest stable version of AutoIT and as I see in this forum post...


Bug Tracker 3110:
https://www.autoitscript.com/trac/autoit/ticket/3110
Version:     3.3.15.0

I believe I'm getting roughly the same error... in the array.au3.
; Title .........: Array
; AutoIt Version : 3.3.14.1

"C:\Program Files (x86)\AutoIt3\Include\array.au3" (2297) : ==> Array variable has incorrect number of subscripts or subscript dimension range exceeded.:
If IsInt($aArray[$iBase]) Then
If IsInt(^ ERROR

As of this post... stable version is...
3.3.14.1 (28th July, 2015) (Release)

Edited by souldjer777

"Maybe I'm on a road that ain't been paved yet. And maybe I see a sign that ain't been made yet"
Song Title: I guess you could say
Artist: Middle Class Rut

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...