how to notice false from 0, possible at all?
Posted: Mon Jul 17, 2017 10:44 pm
I just hunted a long time for a bug in my code until i finally realised what it was.
i had this:
Then there was this other part that was invoking this one:
Thing worked fine, except for one case.
If what I was looking for happened to be in key location 0, then when it returned location=0, it is same as "false", which means that even it actually did find what it was looking for, it didnt do what it was supposed to do, since location<>false, thought it was false situation.
All in all, this is a bit annoying situation when using foreach in this way, and it begs the question: Is there anyway to find out if what i return is a "false" or a "0", or are they simply one and same, in which case i have to make some extra lines to change it into returning string "notfound" instead.
i had this:
Code: Select all
function lookfromtable(lookfor)
location = false
foreach(table, function (key, item)
if item = lookfor then location = key
endfunction
return(location)
Code: Select all
location = lookfromtable("string")
if location<>false
do stuff
else
return(false)
endif
If what I was looking for happened to be in key location 0, then when it returned location=0, it is same as "false", which means that even it actually did find what it was looking for, it didnt do what it was supposed to do, since location<>false, thought it was false situation.
All in all, this is a bit annoying situation when using foreach in this way, and it begs the question: Is there anyway to find out if what i return is a "false" or a "0", or are they simply one and same, in which case i have to make some extra lines to change it into returning string "notfound" instead.