Jump to content

[solved] Expected behavior? stringreplace converts bool to string


Recommended Posts

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 by junkew
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

 

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 here
RegExp tutorial: enough to get started
PCRE 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

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.

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 here
RegExp tutorial: enough to get started
PCRE 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

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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...