I’m trying to check which index is missing in the array. The array is associative, but the indexes are only numeric, although they may not be sequential. At the same time, the size of the array is known. Here is one of the approaches I’ve tried to implement.
:local Array { "0"="Value 1"; "4"="Value 2"; "2"="Value 3"; "3"="Value 5" }
:local arraySize 4
:for i from=0 to=$arraySize do={
:if ([ :typeof ( $Array ->$i ) ] = "nothing") do={
:put ("Index " . $i . " is missing")
}
}
Can't be empty holes on numeric index.
for example, if you create the #7 on one empty array, automatically are created 8 values from 0 to 7
{
:local test [:toarray ""]
:set ($test->7) "seven"
result is 8, from 0 to 7
:put [:len $test]
0 is nil (and nil is not nothing)
:put [:typeof ($test->0)]
:put ($test->0)
7 is the string "seven"
:put [:typeof ($test->7)]
:put ($test->7)
}
{
:local Array { "0"="Value 1"; "4"="Value 2"; "2"="Value 3"; "3"="Value 5" }
:local EXPECTEDarraySize 5
:if ([:len $Array] != $EXPECTEDarraySize) do={:put "Array length unexpected ($[:len $Array] instead of $EXPECTEDarraySize)"}
}
{
:local Array [:toarray ""]
:set ($Array->1) "Value 1"
:set ($Array->2) "Value 2"
:set ($Array->3) "Value 3"
:set ($Array->5) "Value 5"
:for index from=0 to=[:len $Array] do={
:if ([:typeof ($Array ->$index)] = "nil") do={
:put "Index $index is NIL"
}
}
}
But if #5 is not defined, the program do not know the missing #4.