One array can contain all types of data, can only be used "foreach" for search if a sub-array exist.
if ->0 is undefined ("nothing"), but the lenght is > 0, the array have only key pairs,
if last ->n is defined (can also be "nil"), and is the same length (- 1) of the array, the array have only numeric values.
:global x [:toarray ""]
:set ($x->9) 9
:put [:len $x]
:put [:typeof ($x->0)]
:put [:typeof ($x->10)]
:put $x
:global x [:toarray ""]
set ($x->0) 1
set ($x->"test") "test"
set ($x->"atest") [:toarray ""]
:put $x
:put [:typeof ($x->"test")]
:put [:typeof ($x->"atest")]
no matter the order, everytime numeric index be first, and undeclared numbers automatically created for fill the gap,
keyed values are at the end, and everytime sorted (I use this trick to sort items)
:global x [:toarray ""]
:set ($x->"t2") "v2"
:set ($x->2) 2
:set ($x->4) 4
:set ($x->"t1") "v1"
:set ($x->9) 9
:put [:len $x]
:put [:typeof ($x->0)]
:put [:typeof ($x->([:len $x] - 1))]
:put $x
Test if one array contains also keyed values and if one index value are one sub-array, and if one keyed value is also one sub-array.
example code
{
:local x [:toarray ""]
:set ($x->9) test
:put $x
:local multiarray false
:local multikeyarray false
:local numericonly true
:for counter from=0 to=([:len $x] - 1) do={
:if ([:typeof ($x->$counter)] = "array") do={:set multiarray true}
:if ([:typeof ($x->$counter)] = "nothing") do={:set numericonly false}
}
:foreach v,w in=$x do={
:if (([:typeof $v] != "num") and ([:typeof $w] = "array")) do={:set multikeyarray true}
}
:put "multiarray on numeric $multiarray"
:put "multiarray on key value $multikeyarray"
:put "numericonly $numericonly"
:put "\r\n***\r\n"
:set ($x->"test") "test"
:put $x
:local multiarray false
:local multikeyarray false
:local numericonly true
:for counter from=0 to=([:len $x] - 1) do={
:if ([:typeof ($x->$counter)] = "array") do={:set multiarray true}
:if ([:typeof ($x->$counter)] = "nothing") do={:set numericonly false}
}
:foreach v,w in=$x do={
:if (([:typeof $v] != "num") and ([:typeof $w] = "array")) do={:set multikeyarray true}
}
:put "multiarray on numeric $multiarray"
:put "multiarray on key value $multikeyarray"
:put "numericonly $numericonly"
:put "\r\n***\r\n"
:set ($x->4) [:toarray ""]
:put $x
:local multiarray false
:local multikeyarray false
:local numericonly true
:for counter from=0 to=([:len $x] - 1) do={
:if ([:typeof ($x->$counter)] = "array") do={:set multiarray true}
:if ([:typeof ($x->$counter)] = "nothing") do={:set numericonly false}
}
:foreach v,w in=$x do={
:if (([:typeof $v] != "num") and ([:typeof $w] = "array")) do={:set multikeyarray true}
}
:put "multiarray on numeric $multiarray"
:put "multiarray on key value $multikeyarray"
:put "numericonly $numericonly"
:put "\r\n***\r\n"
:set ($x->"something") [:toarray ""]
:put $x
:local multiarray false
:local multikeyarray false
:local numericonly true
:for counter from=0 to=([:len $x] - 1) do={
:if ([:typeof ($x->$counter)] = "array") do={:set multiarray true}
:if ([:typeof ($x->$counter)] = "nothing") do={:set numericonly false}
}
:foreach v,w in=$x do={
:if (([:typeof $v] != "num") and ([:typeof $w] = "array")) do={:set multikeyarray true}
}
:put "multiarray on numeric $multiarray"
:put "multiarray on key value $multikeyarray"
:put "numericonly $numericonly"
}
and for the end of test, if [:typeof ($x->0)] is "nothing" but the length is > 0, the array do not have numeric values, but only keyed pairs.