Sign in to follow this  
Followers 0
sksbir

numbers and booleans with iniread

7 posts in this topic

#1 ·  Posted (edited)

Some behaviour of iniread may conduct to mistake :

I found my solution here: http://www.autoitscript.com/forum/index.ph...amp;mode=linear

In the same manner :

ini file:

MYVAR=0

script:

$MYVAR=iniread(inifile,"xx","MYVAR",0)

if $MYVAR then

msgbox(0,"","this should not be displayed")

endif

the message box is displayed because iniread gives string "0" which is true.

$MYVAR=number(iniread(inifile,"xx","MYVAR")) will solve the problem too.

Edited by sksbir

Share this post


Link to post
Share on other sites



You need to specify four parameters for IniRead function.

IniRead ( "filename", "section", "key", "default" )

Take a good look at "default" parameter. It is the default value to return if the requested key is not found.

Share this post


Link to post
Share on other sites

@manadar : you are right, here is the correction:

$MYVAR=iniread(inifile,"xx","MYVAR",0)

but the problem is here: without using the "number" function, behaviour of iniread my conduct to mistake.

Adding MYVAR=0 in inifile will give $MYVAR a true boolean value, altough 0 is False in autoit scripts.

Share this post


Link to post
Share on other sites

Actually, this behavior is seen in many AutoIt functions. The internal function requires a string to work, so it casts a number to a string and works with that. It will also return a string then.

In this case that is the "default" parameter. It is converted into a string. You will see that the return value of the script is the string: "0". If you are expecting a string, you can simple cast it using the Number($MYVAR) function, to guarantee a number.

This is one of the challenges of using AutoIt, I guess. Not really an error.

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

.....This is one of the challenges of using AutoIt, I guess. Not really an error....

ok, not really, BUT.....

when you use this instruction for example:

$TEST=iniread($INIFILE,"TEST","TEST",0)

in case there is no TEST value in your ini file, you expect to get back the default value. BUT... the default value is not 0, but "0"

if you write this:

$TEST=iniread($INIFILE,"TEST","TEST",0)

if $TEST then

do something

endif

you will ALWAYS do something,even with no ini file at all...

And

$TEST=iniread($INIFILE,"TEST","TEST",0)

should not be the same as

$TEST=iniread($INIFILE,"TEST","TEST","0")

but in fact, it is.

So I don't say that this is a bug, but this should be clearly explained in autoit documentation, with some "good" examples

Edited by sksbir

Share this post


Link to post
Share on other sites

Experience says that if you're using anything but a bool to go into an if-then-else statement you're going to run into unexpected results. Do a proper check for instead if the result $MYVAR == "0" is true and not just $MYVAR or even $MYVAR = "0".

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

Experience says that if you're using anything but a bool to go into an if-then-else statement you're going to run into unexpected results. Do a proper check for instead if the result $MYVAR == "0" is true and not just $MYVAR or even $MYVAR = "0".

I understand you point of view, but I have often seen examples in autoit help that just show the opposite.

Take a look at the example script of findfirstfile for example :

You will find this:

$file = FileFindNextFile($search)

If @error Then ExitLoop

with your principle, it should be:

$file = FileFindNextFile($search)

If @error<>0 Then ExitLoop

an other example: Filegetattrib

If StringInStr($attrib, "R") then...

should be:

If StringInStr($attrib, "R")>0 then...

so I can legitimate think that peoples who developped autoit aim to get more visible source code, even if the resulting code is not the "state of the art"...

Has enhancement suggestion for autoit, I think that iniread should return a variable type in the same kind of the default value.

iniread(file,section,variable,0) should return an integer

iniread(file,section,variable,"0") should return a string

iniread(file,section,variable,False) should return a boolean

Edited by sksbir

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0