Reputation: 902
So simply as the name suggests... You can't say like
3[7] --> Attempt to index a number value
"fpp"[7] --> Attempt to index a string value
I know the function type()
, but I am trying to avoid it, because it's slow.
if(type({}) == "table") ...
if(string.sub(tostring({}),1,5) == "table")...
function ArrayCount(arArr)
if(not arArr) then return 0 end
if(not (type(arArr) == "table")) then return 0 end
if(not (arArr and arArr[1])) then return return 0 end
local Count = 1
while(arArr[Count]) do Count = Count + 1 end
return (Count - 1)
end
ArrayCount(3)
ArrayCount("I am a string!")
Upvotes: 1
Views: 2538
Reputation: 80657
The type
function is not slow. Update your function to simply:
function ArrayCount(arArr)
if type( arArr ) ~= "table" then return 0 end
return #arArr - 1
end
Upvotes: 4