How to check if a simple array is empty. Topic is solved
How to check if a simple array is empty.
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.
- Here's some code:
- In AHK v1, a linear array is an associative array with positive integer keys only, starting at 1, with no gaps.
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()
}
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Re: How to check if a simple array is empty.
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?
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.
Then there is no way other than iterating over all the elements and checking if they fit your definition of "empty".
Recommends AHK Studio
Re: How to check if a simple array is empty.
how do you do that, with the HasKey function?
Re: How to check if a simple array is empty.
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
No you use a for loop and check each and every single value if it fits your criteria:
Or as a function:
Edit: <> is deprecated and shouldnt be used in new scripts.
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
}
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
}
Recommends AHK Studio
Re: How to check if a simple array is empty.
<> 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.
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.
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
Re: How to check if a simple array is empty.
thank you very much for all your help
Re: How to check if a simple array is empty.
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
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.
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