Jump to content

Finding top 4 or 3 or 2 values in an array


Recommended Posts

I have an array that gets updated each time data in entered

local $array[12]=[1,9,11,5,1,12,10,16,8,1,9,3]

I need to know when when 4 of the elements have hit 10 or greater, so from the above I would like to be told that

indexes 2,5,6,7 have met the criteria as there are 4 elements of 10 or greater.

How do I perform a check each time data is entered to find the top 4 values? I can get my head around it. I will also be doing the same on the top 3 and top 2 but assume top 4 would be the strating point to understand it.

Edited by Phaser
Link to post
Share on other sites

Do you just have to know the number of elements or the indices of the elements > 10?

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2021-06-05 - Version 1.5.4.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2021-06-14 - Version 1.6.5.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
Task Scheduler (2019-12-03 - Version 1.5.1.0) - Download - General Help & Support - Wiki

Tutorials:
ADO - Wiki, WebDriver - Wiki

 

Link to post
Share on other sites

You would need something like this:

#include <array.au3>

Global $iLimit = 10
Global $iCount = 0
Global $aInput[12] = [1,9,11,5,1,12,10,16,8,1,9,3]
Global $aIndices[UBound($aInput)]

For $i = 0 To UBound($aInput)-1
    If $aInput[$i] >= $iLimit Then
        For $j = 0 To $iCount
            If $aIndices[$j] = $aInput[$i] Then ContinueLoop
            If $aIndices[$j] = "" Then
                $aIndices[$j] = $aInput[$i]
                $iCount = $iCount + 1
                ExitLoop
            EndIf
        Next
    Endif
Next
_ArrayDisplay($aIndices)
ConsoleWrite("# of indices >= " & $iCount & @CRLF)

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2021-06-05 - Version 1.5.4.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2021-06-14 - Version 1.6.5.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
Task Scheduler (2019-12-03 - Version 1.5.1.0) - Download - General Help & Support - Wiki

Tutorials:
ADO - Wiki, WebDriver - Wiki

 

Link to post
Share on other sites

Try this.

Local $aInput[12] = [1, 9, 11, 5, 1, 12, 10, 16, 8, 1, 9, 3]
Local $iLimit = 10
Local $iCount = 0
Local $sIndices = ""

For $i = 0 To UBound($aInput) - 1
    If $aInput[$i] >= $iLimit Then
        $iCount += 1
        If $iCount <= 4 Then
            $sIndices &= $i & ","
        Else
            ExitLoop
        EndIf
    EndIf
Next

ConsoleWrite("The indices with values >= 10 are " & StringTrimRight($sIndices, 1) & "." & @CRLF)

#cs
Output is:-
The indices with values >= 10 are 2,5,6,7.
#ce
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
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...