Page 1 of 1

How to check if a simple array is empty.

Posted: 28 Feb 2019, 14:08
by KilliK
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.

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

Posted: 28 Feb 2019, 14:18
by Helgef

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

Posted: 28 Feb 2019, 14:40
by jeeswg
- 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.

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

Posted: 01 Mar 2019, 08:02
by KilliK
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?

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

Posted: 01 Mar 2019, 09:01
by nnnik
Then there is no way other than iterating over all the elements and checking if they fit your definition of "empty".

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

Posted: 01 Mar 2019, 10:55
by KilliK
how do you do that, with the HasKey function?

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

Posted: 01 Mar 2019, 11:17
by just me
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%

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

Posted: 01 Mar 2019, 11:20
by nnnik
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.

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

Posted: 01 Mar 2019, 11:29
by just me
<> has been removed from v2 without any serious reason. Therefore it's called 'depricated' in v1 without a reason, too.

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

Posted: 01 Mar 2019, 12:44
by nnnik
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.

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

Posted: 04 Mar 2019, 12:35
by KilliK
thank you very much for all your help :bravo:

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

Posted: 01 Nov 2023, 20:21
by Nixcalo
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.