Jump to content
Sign in to follow this  
Synitare

Schrödinger's variable

Recommended Posts

Synitare

I am fairly new to the scripting world, but I've run into an issue that I cannot understand. I have a script in which I initialize a variable as 0 so that it can be checked later for any change. Initially the value changed to another Int, so there was never an issue, but later I modified the script to change the data to a text string but left the initialized variable as 0 instead of "" simply because it's just a simple quick 'n' dirty script that will be discarded soon.

Later in the script I have an If...Then statement that checks if the variable is still 0, and if it is will end the script with an error. The issue is that the If statement somehow still triggers even if the variable changes, but the variable will simultaneously read correctly as whatever it was changed to. An example to illustrate what I mean:

Local $var = 0
$var = "Test"

If $var = 0 Then MsgBox(4096, "", "The variable is: " & $var)

This only appears to happen when the initial value is set to 0 and is then changed to a text string. If it is any other int or text string or if the value changes to another integer, everything works as I would expect. Is this intended behavior? Am I missing something?

I tried searching around was unable to find anything, so here I am.

Share this post


Link to post
Share on other sites
FireFox

Hi,

AutoIt interperts the variables differently from other languages, you are comparing a string to an integer, the string is converted to an integer and since it's not an interpretable number it's converted to 0.

Br, FireFox.


 

OS : Win XP SP2 (32 bits) / Win 7 SP1 (64 bits) / Win 8 (64 bits) | Autoit version: latest stable / beta.
Hardware : Intel(R) Core(TM) i5-2400 CPU @ 3.10Ghz / 8 GiB RAM DDR3.

My UDFs : Skype UDF | TrayIconEx UDF | GUI Panel UDF | Excel XML UDF | Is_Pressed_UDF

My Projects : YouTube Multi-downloader | FTP Easy-UP | Lock'n | WinKill | AVICapture | Skype TM | Tap Maker | ShellNew | Scriptner | Const Replacer | FT_Pocket | Chrome theme maker

My Examples : Capture toolIP Camera | Crosshair | Draw Captured Region | Picture Screensaver | Jscreenfix | Drivetemp | Picture viewer

My Snippets : Basic TCP | Systray_GetIconIndex | Intercept End task | Winpcap various | Advanced HotKeySet | Transparent Edit control

 

Share this post


Link to post
Share on other sites
AndyG

....and you should use someting like IsString($var) to be sure that $var is a string :)

Share this post


Link to post
Share on other sites
Synitare

Thanks all for the information. Especially the bit about the double equal. Totally slipped my mind.

To check my understanding of the answer from FireFox; this occurs because the variable is initially interpreted as an integer but since it later changes to a string, that string gets converted to an integer; which evidently is always zero since its not a valid integer; thus causing any conditional checking whether it is zero to trigger while simultaneously evaluating the variable correctly when called? This would explain why any int other than zero seems to work just fine.

Edited by Synitare

Share this post


Link to post
Share on other sites
Xandy

Mathematically "Test"= 0

Once you reassign $var to "Test" the past value of 0 is lost!

Edited by Xandy

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  

×