how to notice false from 0, possible at all?

Discuss any general programming issues here
Post Reply
Bugala
Posts: 792
Joined: Sun Feb 14, 2010 7:11 pm

how to notice false from 0, possible at all?

Post by Bugala » 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:

Code: Select all

function lookfromtable(lookfor)
location = false
foreach(table, function (key, item)
                           if item = lookfor then location = key
                       endfunction
return(location)
Then there was this other part that was invoking this one:

Code: Select all

location = lookfromtable("string")
if location<>false
   do stuff
else
   return(false)
endif
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.

peceha
Posts: 111
Joined: Tue Dec 13, 2016 8:39 am
Location: Poland

Re: how to notice false from 0, possible at all?

Post by peceha » Thu Jul 20, 2017 3:40 pm

Hi,

in the manual you can find following statement:
True and False are inbuilt constants which have the values of 1 (True) and 0 (False).
So I belive it is not possible.

Bugala
Posts: 792
Joined: Sun Feb 14, 2010 7:11 pm

Re: how to notice false from 0, possible at all?

Post by Bugala » Thu Jul 20, 2017 10:51 pm

Well, after thinking about it better, i guess i can start using -1 as the false state. That way, where ever the item happens to be on a list, it will always be something from 0 to positive, hence never colliding with -1.

Post Reply