junkew Posted May 2, 2019 Share Posted May 2, 2019 (edited) Is below expected behavior? Even if there is nothing to replace I get a bool converted to a string when i us a string function on a bool variable I had a challenging issue in automation udf I am working on which boils down to local $Val=True ConsoleWrite(VarGetType($Val) & stringlen($val) & isbool($val) & isstring($val) & @CRLF) $Val=stringreplace($Val, "a","") ConsoleWrite(VarGetType($Val) & stringlen($val) & isbool($val) & isstring($val) & @CRLF) In my actual code I was reading from a configuration file With logic like below where I get an unwanted conversion from bool to string (in case the value is true) as that breaks if statements on boolean logic. Fixed it by moving the bool logic to lower part in the coding ... If StringLower($strVal) = "true" Then $strVal = True If StringLower($strVal) = "false" Then $strVal = False If StringLower($strVal) = "on" Then $strVal = True If StringLower($strVal) = "off" Then $strVal = False If StringLower($strVal) = "minimized" Then $strVal = @SW_MINIMIZE If StringLower($strVal) = "maximized" Then $strVal = @SW_MAXIMIZE If StringLower($strVal) = "normal" Then $strVal = @SW_RESTORE //TODO: Move to top $strVal = StringReplace($strVal, "%windowsdir%", @WindowsDir) $strVal = StringReplace($strVal, "%programfilesdir%", @ProgramFilesDir) ... Edited May 2, 2019 by junkew FAQ 31 How to click some elements, FAQ 40 Test automation with AutoIt, Multithreading CLR .NET Powershell CMDLets Link to comment Share on other sites More sharing options...
user4157124 Posted May 2, 2019 Share Posted May 2, 2019 Not ideal but expected (consistent behavior). Manual conversion (StringLower) isn't required however: ConsoleWrite(('False' = 'false') & @CRLF) Empty str evaluates to False, so could just do: $vValue = String(False) $vValue = $vValue = 'false' ? '' : $vValue If $vValue Then ConsoleWrite('True' & @CRLF) Else ConsoleWrite('False' & @CRLF) EndIf AUERLO (AutoIt error logger) Link to comment Share on other sites More sharing options...
jchd Posted May 2, 2019 Share Posted May 2, 2019 1 hour ago, junkew said: Even if there is nothing to replace I get a bool converted to a string when i us a string function on a bool variable This the effect of AutoIt duck typing. Internal functions explicitely expecting a type force a conversion of the argument into the wanted type. The multiple If block can be: Switch StringLower($strVal) Case 'maximized' $strVal = @SW_MAXIMIZE Case 'minimized' $strVal = @SW_MINIMIZE Case 'normal' $strVal = @SW_RESTORE Case 'false', 'off', '0', '' $strVal = False Case Else $strVal = True EndSwitch Skysnake and FrancescoDiMuro 2 This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe hereRegExp tutorial: enough to get startedPCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta. SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt) Link to comment Share on other sites More sharing options...
junkew Posted May 2, 2019 Author Share Posted May 2, 2019 ok thx guys. frustrating when you store different types of data in array and types get mangled when you iterate over the array with string functions. Copied the switch which is indeed better to use. FAQ 31 How to click some elements, FAQ 40 Test automation with AutoIt, Multithreading CLR .NET Powershell CMDLets Link to comment Share on other sites More sharing options...
jchd Posted May 5, 2019 Share Posted May 5, 2019 I know you know, but for other readers: Indeed, AutoIt will perform thru the loop as dictated by the code using e.g. string functions, applying stringizing input data as required. That's a consequence of using variants under the hood. This may seem uncomfortable or even odd, but if you compare with doing blindly the same in strict-typed languages (say C), you'd get anything from garbage out to a hard crash. In fact, in C (or other strict-typed languages) you'd have to switch according to datatype to perform only sensible operations. Advice: as far as possible store only a consistent datatype in arrays. If you really can't, just switch inside the loop using VarGetType. Skysnake 1 This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe hereRegExp tutorial: enough to get startedPCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta. SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt) 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