Page 1 of 1

How to return an array position

Posted: 17 Apr 2018, 21:41
by songdg
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

Re: How to return an array position  Topic is solved

Posted: 17 Apr 2018, 23:10
by wolf_II
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.

Re: How to return an array position

Posted: 18 Apr 2018, 21:42
by songdg
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.

Re: How to return an array position

Posted: 15 Feb 2021, 14:57
by P0gn1

Code: Select all

MyArray := ["some text",1,"some text",1,"some text"]
result := ArrayGetPos(MyArray, 1) ; return [2,4]
result := ArrayGetPos(MyArray, "some text" ; return [1,3,5]

result := ArrayGetPos(MyArray, 1)[1] ; to get first pos in array
ExitApp

ArrayGetPos(array, search) {
    res := []
    Loop  {
    	if (!array[A_Index]) {
    	break
    	}
        if (array[A_Index] = search) {
            res.Push(A_Index)
        }
    }
    return res
}
I think this is better, beacuse this func can return all pos, not only 1