reading and writing booleans

Has anyone had any experience writing and reading booleans from a file?
I seem to get inconsistent results using the following code:

local value1 = true

file:write( tostring( var value1) )

value1 = file:read()

I print out value1 and it seems to be correct but when I attempt to test it or use it, it does not not seem to work properly.
For example:

if value1 == true then

end [import]uid: 108253 topic_id: 19674 reply_id: 319674[/import]

Try tostring(value1) when writing to file.

EDIT:
Also, when you read back the value from the file, you get a string. You’ll need to convert it back to a Boolean. [import]uid: 70847 topic_id: 19674 reply_id: 76117[/import]

Well from my example I already tried using tostring(value1).
It’s ther converting back to boolean that has me stumped.
Right now I am using the following clunky code:

local line = file:read()

boolean val = false
if line – “true” then
val = true
end

Very clunky. Must be a a better way? [import]uid: 108253 topic_id: 19674 reply_id: 76204[/import]

Untested, but works in most languages:

boolean val = (line == "true")

You may like to switch to using 0,1 instead of false,true [import]uid: 108660 topic_id: 19674 reply_id: 76210[/import]

In Lua there are no strict types assigned to variables.
The type is determined upon evaluation, so 0 and 1 will not work since they will always evaluate to a numeric. Anything <> nil is always evaluated as true.

The code val = (line == "true") will work perfectly though. [import]uid: 70847 topic_id: 19674 reply_id: 76214[/import]

Thank you all.

The line:

val = ( line == “true”) seems to be the best option. It at least makes the code more readable and seemingly compact.

Cheers,
-Dennis
[import]uid: 108253 topic_id: 19674 reply_id: 76513[/import]