How to check if all values in an Array are equal? Topic is solved
How to check if all values in an Array are equal?
Hello.
How can I check if all the values in an Array are equal or not? and when they are equal, to get their identical value?
for example
they are all equal with the same value A
Array:=["A","A","A","A","A"]
they are not all equal:
Array:=["A","A","B","A","A"]
what is the simplest method to achieve this?
How can I check if all the values in an Array are equal or not? and when they are equal, to get their identical value?
for example
they are all equal with the same value A
Array:=["A","A","A","A","A"]
they are not all equal:
Array:=["A","A","B","A","A"]
what is the simplest method to achieve this?
Re: How to check if all values in an Array are equal? Topic is solved
Code: Select all
For arrNum, arr in [["A","A","A","A","A"], ["A","A","B","A","A"], ["", "C"], ["", ""], ["L"]] {
test := same(arr)
MsgBox, 64, Result #%arrNum%, % "Same: " (test.equal ? "True" : "False") "`n`nValue: " test.value
}
same(arr) {
For itemNum, item in arr
If (itemNum = 1 || item = value)
value := item
Else Return {value: "", equal: False}
Return {value: value, equal: True}
}
Last edited by mikeyww on 26 Jun 2022, 06:51, edited 1 time in total.
Re: How to check if all values in an Array are equal?
If array values are numeric, min(a*) == max(a*)
Cheers
Edit, alternative, case sensitive,
Cheers
Edit, alternative, case sensitive,
Code: Select all
same(arr) {
v1 := arr[1]
for k, v in arr
if !(v1==v)
return false
return true
}
Re: How to check if all values in an Array are equal?
thank you very much for all your help.
Re: How to check if all values in an Array are equal?
https://biga-ahk.github.io/biga.ahk/#/?id=isequal approach
One other benefit is it works with objects too:
Code: Select all
A := new biga() ; requires https://biga-ahk.github.io/biga.ahk
arr1 := ["A","A","A","A","A"]
arr2 := ["A","A","B","A","B"]
msgbox, % A.isEqual(arr1*)
; => true
msgbox, % A.isEqual(arr2*)
; => false
One other benefit is it works with objects too:
Code: Select all
obj1 := {"name": "Zues"}
obj2 := {"name": "Jupiter"}
msgbox, % A.isEqual(obj1, obj2)
; => false
- flyingDman
- Posts: 2817
- Joined: 29 Sep 2013, 19:01
Re: How to check if all values in an Array are equal?
Or:
Code: Select all
For arrNum, arr in [["A","A","A","A","A"], ["A","A","B","A","A"], ["", "C"], ["", ""], ["L"]]
msgbox % same(arr)
same(array)
{
arr0 := []
for x,y in array
arr0[y] := !ObjHasKey(arr0, y) ? 1 : arr0[y] + 1
return arr0.count() = 1 ? "True" : "False"
}
14.3 & 1.3.7
Re: How to check if all values in an Array are equal?
All good. When the returned value is true (i.e., elements are the same), then the value will be the value of the first array element (or any of them).
Re: How to check if all values in an Array are equal?
Code: Select all
"True" : "False"