How to return an array position Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
songdg
Posts: 57
Joined: 04 Oct 2017, 20:04

How to return an array position

17 Apr 2018, 21:41

An array consist of seven elements,I want to return the position like this

Code: Select all

;if all the elements is the same return the first position
;return the first position of the elements which occur most 
arr := [111, 111, 111, 111, 111, 111, 111]      ;return 1
arr := [123, 111, 111, 111, 111, 111, 111]      ;return 2
arr := [112, 119, 111, 111, 111, 111, 111]      ;return 3
arr := [117, 116, 115, 111, 111, 111, 111]      ;return 4
arr := [117, 118, 111, 111, 189, 111, 111]      ;return 3
arr := [117, 119, 111, 111, 111, 111, 177]      ;return 3
wolf_II
Posts: 2273
Joined: 08 Feb 2015, 20:55

Re: How to return an array position  Topic is solved

17 Apr 2018, 23:10

Try this:

Code: Select all

;if all the elements is the same return the first position
;return the first position of the elements which occur most
arr1 := [111, 111, 111, 111, 111, 111, 111]      ;return 1
arr2 := [123, 111, 111, 111, 111, 111, 111]      ;return 2
arr3 := [112, 119, 111, 111, 111, 111, 111]      ;return 3
arr4 := [117, 116, 115, 111, 111, 111, 111]      ;return 4
arr5 := [117, 118, 111, 111, 189, 111, 111]      ;return 3
arr6 := [117, 119, 111, 111, 111, 111, 177]      ;return 3

Loop, 6
    MsgBox, % PosOf_MostItem(arr%A_Index%)

ExitApp



;-------------------------------------------------------------------------------
PosOf_MostItem(List) { ; return first position of the most frequent item in List
;-------------------------------------------------------------------------------
    Table := []
    For index, Candidate in List
        If Table.hasKey(Candidate)
            Table[Candidate].Score++
        Else
            Table[Candidate] := {Score: 1, firstOccurrance: index}

    Maximum := 0
    For each, Entry in Table
        If (Entry.Score > Maximum)
            Maximum := Entry.Score
          , indexOf_Max := Entry.firstOccurrance

    Return, indexOf_Max
}
I hope that helps.
songdg
Posts: 57
Joined: 04 Oct 2017, 20:04

Re: How to return an array position

18 Apr 2018, 21:42

wolf_II wrote:Try this:

Code: Select all

;if all the elements is the same return the first position
;return the first position of the elements which occur most
arr1 := [111, 111, 111, 111, 111, 111, 111]      ;return 1
arr2 := [123, 111, 111, 111, 111, 111, 111]      ;return 2
arr3 := [112, 119, 111, 111, 111, 111, 111]      ;return 3
arr4 := [117, 116, 115, 111, 111, 111, 111]      ;return 4
arr5 := [117, 118, 111, 111, 189, 111, 111]      ;return 3
arr6 := [117, 119, 111, 111, 111, 111, 177]      ;return 3

Loop, 6
    MsgBox, % PosOf_MostItem(arr%A_Index%)

ExitApp



;-------------------------------------------------------------------------------
PosOf_MostItem(List) { ; return first position of the most frequent item in List
;-------------------------------------------------------------------------------
    Table := []
    For index, Candidate in List
        If Table.hasKey(Candidate)
            Table[Candidate].Score++
        Else
            Table[Candidate] := {Score: 1, firstOccurrance: index}

    Maximum := 0
    For each, Entry in Table
        If (Entry.Score > Maximum)
            Maximum := Entry.Score
          , indexOf_Max := Entry.firstOccurrance

    Return, indexOf_Max
}
I hope that helps.
Thanks,much appreaciated.

Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot], Hellbent, loek3000, zapal and 99 guests