sksbir Posted March 20, 2009 Share Posted March 20, 2009 (edited) Some behaviour of iniread may conduct to mistake :I found my solution here: http://www.autoitscript.com/forum/index.ph...amp;mode=linearIn the same manner :ini file:MYVAR=0script:$MYVAR=iniread(inifile,"xx","MYVAR",0)if $MYVAR then msgbox(0,"","this should not be displayed")endifthe message box is displayed because iniread gives string "0" which is true.$MYVAR=number(iniread(inifile,"xx","MYVAR")) will solve the problem too. Edited March 23, 2009 by sksbir Link to comment Share on other sites More sharing options...
jvanegmond Posted March 20, 2009 Share Posted March 20, 2009 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. github.com/jvanegmond Link to comment Share on other sites More sharing options...
sksbir Posted March 20, 2009 Author Share Posted March 20, 2009 @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. Link to comment Share on other sites More sharing options...
jvanegmond Posted March 20, 2009 Share Posted March 20, 2009 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. github.com/jvanegmond Link to comment Share on other sites More sharing options...
sksbir Posted March 20, 2009 Author Share Posted March 20, 2009 (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 somethingendifyou 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 March 20, 2009 by sksbir Link to comment Share on other sites More sharing options...
jvanegmond Posted March 20, 2009 Share Posted March 20, 2009 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". github.com/jvanegmond Link to comment Share on other sites More sharing options...
sksbir Posted March 23, 2009 Author Share Posted March 23, 2009 (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 ExitLoopwith your principle, it should be:$file = FileFindNextFile($search) If @error<>0 Then ExitLoopan other example: FilegetattribIf 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 integeriniread(file,section,variable,"0") should return a stringiniread(file,section,variable,False) should return a boolean Edited March 23, 2009 by sksbir Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now