How to check if a simple array is empty. Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
KilliK
Posts: 236
Joined: 10 Mar 2016, 21:19

How to check if a simple array is empty.

28 Feb 2019, 14:08

Sorry for my ignorant question, but do I check if a simple array (Array:=[]) is completely empty? So far I have found only how to do it for associative arrays.
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: How to check if a simple array is empty.

28 Feb 2019, 14:40

- Here's some code:

Code: Select all

;to check if an associative array or linear array is empty:
MsgBox, % oArray.Count() ;will silently fail prior to AHK v1.1.29
MsgBox, % ObjCount(oArray)

;to check if a linear array is empty:
;(this returns the number of the largest positive integer key for a linear or associative array)
MsgBox, % oArray.Length()
MsgBox, % ObjLength(oArray)

;note: obj.Count() requires AHK v1.1.29+
oArray1 := ["a", "b", "c"] ;linear [and associative]
oArray2 := {1:"a", 2:"b", 3:"c"} ;linear [and associative] (same array as above)
oArray3 := {1:"a", 2:"b", 3:"c", 5:"e"} ;associative (there is a gap)
oArray4 := {1:"a", 2:"b", 3:"c", x:"_"} ;associative (there is a non-integer)
oArray5 := {1:"a", 2:"b", 3:"c", -5:"_"} ;associative (there is a negative integer)
oArray6 := {1:"a", 2:"b", 3:"c", 0:"_"} ;associative (there is 0)
oArray7 := [] ;linear [and associative]
oArray8 := {} ;linear [and associative] (same array as above)
Loop, 8
{
	oArray := oArray%A_Index%
	MsgBox, % oArray.Length() " " oArray.Count()
}
- In AHK v1, a linear array is an associative array with positive integer keys only, starting at 1, with no gaps.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
KilliK
Posts: 236
Joined: 10 Mar 2016, 21:19

Re: How to check if a simple array is empty.

01 Mar 2019, 08:02

Thank you for the help, all the suggestions work fine.
But there is one particular case that they dont work. When the array does have values but they are all empty.
For example if I have this Array:=["","",""], how do I check that all its values are empty?
User avatar
nnnik
Posts: 4480
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: How to check if a simple array is empty.

01 Mar 2019, 09:01

Then there is no way other than iterating over all the elements and checking if they fit your definition of "empty".
Recommends AHK Studio
KilliK
Posts: 236
Joined: 10 Mar 2016, 21:19

Re: How to check if a simple array is empty.

01 Mar 2019, 10:55

how do you do that, with the HasKey function?
just me
Posts: 7583
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: How to check if a simple array is empty.

01 Mar 2019, 11:17

You want to check values, not keys.

Code: Select all

IsEmpty := True
For Key, Value In MyObject {
   If (Value <> "") {
      IsEmpty := False
      Break
   }
}
MsgBox, IsEmpty = %IsEmpty%
User avatar
nnnik
Posts: 4480
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: How to check if a simple array is empty.  Topic is solved

01 Mar 2019, 11:20

No you use a for loop and check each and every single value if it fits your criteria:

Code: Select all

array := [] ;.. actual array can have different values

isEmpty := true
for each, value in array {
	if !(value == "") {
		isEmpty := false
		break
	}
}

if (isEmpty) {
	Msgbox Array is empty
} else {
	Msgbox Array is not empty
}
Or as a function:

Code: Select all

isEmptyOrEmptyStringsOnly(inputArray) {
	for each, value in inputArray {
		if !(value == "") {
			return false ;one of the values is not an empty string therefore the array is not empty or empty strings only
		}
	}
	return true ;all the values have passed the test or no values where inside the array
}
array := [] ;.. actual array can have different values
if (isEmptyOrEmptyStringsOnly(array)) {
	Msgbox Array is empty
} else {
	Msgbox Array is not empty
}
Edit: <> is deprecated and shouldnt be used in new scripts.
Recommends AHK Studio
just me
Posts: 7583
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: How to check if a simple array is empty.

01 Mar 2019, 11:29

<> has been removed from v2 without any serious reason. Therefore it's called 'depricated' in v1 without a reason, too.
User avatar
nnnik
Posts: 4480
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: How to check if a simple array is empty.

01 Mar 2019, 12:44

It's called deprecated because it has been removed in future versions.
AHK v2 generally aims to unify the amount of possible syntaxes that are available into one possible way of doing it.
<> was removed in favor of != and !==.

Even if you twist the facts to the point that <> was removed without reason it was removed and thus deprecated in v1 and shouldn't be used anymore.
Recommends AHK Studio
KilliK
Posts: 236
Joined: 10 Mar 2016, 21:19

Re: How to check if a simple array is empty.

04 Mar 2019, 12:35

thank you very much for all your help :bravo:

Return to “Ask For Help”

Who is online

Users browsing this forum: Gio, HiSoKa, Joerbeth, mikeyww, mildruchu, vanil, watagan and 55 guests