Jump to content
Sign in to follow this  
Phaser

Finding top 4 or 3 or 2 values in an array

Recommended Posts

Phaser

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

Share this post


Link to post
Share on other sites
water

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 2018-10-19 - Version 1.4.10.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - 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
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
Phaser

No, I need to know that there are 4 of 10 or greater and the indices they are at, thats why I cant get my head around it

Share this post


Link to post
Share on other sites
water

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 2018-10-19 - Version 1.4.10.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - 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
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
Phaser

Many thanks water, I will see if I can work with that, thanks for your help

Share this post


Link to post
Share on other sites
Malkey

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

Share this post


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
Sign in to follow this  

×