How to check if all values in an Array are equal? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
KilliK
Posts: 255
Joined: 10 Mar 2016, 21:19

How to check if all values in an Array are equal?

Post by KilliK » 26 Jun 2022, 05:36

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?

User avatar
mikeyww
Posts: 26848
Joined: 09 Sep 2014, 18:38

Re: How to check if all values in an Array are equal?  Topic is solved

Post by mikeyww » 26 Jun 2022, 06:42

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.

Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: How to check if all values in an Array are equal?

Post by Helgef » 26 Jun 2022, 06:46

If array values are numeric, min(a*) == max(a*)

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
}

User avatar
KilliK
Posts: 255
Joined: 10 Mar 2016, 21:19

Re: How to check if all values in an Array are equal?

Post by KilliK » 26 Jun 2022, 09:32

thank you very much for all your help.

User avatar
Chunjee
Posts: 1417
Joined: 18 Apr 2014, 19:05
Contact:

Re: How to check if all values in an Array are equal?

Post by Chunjee » 26 Jun 2022, 10:12

https://biga-ahk.github.io/biga.ahk/#/?id=isequal approach

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

User avatar
flyingDman
Posts: 2817
Joined: 29 Sep 2013, 19:01

Re: How to check if all values in an Array are equal?

Post by flyingDman » 26 Jun 2022, 13:45

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

User avatar
mikeyww
Posts: 26848
Joined: 09 Sep 2014, 18:38

Re: How to check if all values in an Array are equal?

Post by mikeyww » 26 Jun 2022, 13:58

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).


Post Reply

Return to “Ask for Help (v1)”