How to check if a simple array is empty.
Posted: 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.
Let's help each other out
https://www.autohotkey.com/boards/
https://www.autohotkey.com/boards/viewtopic.php?f=76&t=62428
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()
}
Code: Select all
IsEmpty := True
For Key, Value In MyObject {
If (Value <> "") {
IsEmpty := False
Break
}
}
MsgBox, IsEmpty = %IsEmpty%
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
}
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