Jump to content

Finding top 4 or 3 or 2 values in an array


Phaser
 Share

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 comment
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 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.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
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
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 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.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
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
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 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...