How to check if a simple array is empty. 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 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
User avatar
KilliK
Posts: 255
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: 4500
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
User avatar
KilliK
Posts: 255
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: 9425
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: 4500
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: 9425
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: 4500
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
User avatar
KilliK
Posts: 255
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:
Nixcalo
Posts: 116
Joined: 06 Feb 2018, 04:24

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

01 Nov 2023, 20:21

Why the "==" in the code? Shouldn't a simple "=" in if !(value == "") { suffice, like this if (value != "")
or is there a difference I am missing? Because it seems a way of overcomplicating things...

Also, I am finding a strange behaviour for the function script, something I am not able to explain. I have modified the script to check three different arrays (and removed unnecessary brackets for shorter script length and improved clarity)

If we have this

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 := [234, "345", df, {"sd":23}, "asd", {ef:"sf"}] ; an example of a strange array
arrayempty:=[]
arrayunknown=["","","",,] ; I confess I don't know if this is considered an empty string or not.
if (isEmptyOrEmptyStringsOnly(array)) 
	Msgbox array is empty
else 
	Msgbox array is not empty

if (isEmptyOrEmptyStringsOnly(arrayempty)) 
	Msgbox arrayempty is empty
else 
	Msgbox arrayempty is not empty

if (isEmptyOrEmptyStringsOnly(arrayunknown)) 
	Msgbox arrayunknown is empty
else 
	Msgbox arrayunknown is not empty
the function works perfectly (apparently considers that arrayunknown is empty). Same happens if we use if !(value = "") instead. Same if we use if (value != ""). However, if we use if (value !== ""), the script does not work, everything is empty, and I don't understand why. And, surprisingly (at least to me), if we use both if (value) and if not(value), the results seem to work as well! This blows my mind, and I don't understand AHK behaviour.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot], roysubs, skeerrt and 156 guests