jaberwacky Posted July 21, 2010 Posted July 21, 2010 My question is about a particular method of swapping variables. $x -= $y $y += $x $x = $y - $x My question involves the last line. $x is subtracted from $y and then the result is stored back into $x. In order to store the result back into $x then wouldn't there need to be a variable to store the result temporarily? Otherwise the result would be lost right.? Helpful Posts and Websites: AutoIt3 Variables and Function Parameters MHz | AutoIt Wiki | Using the GUIToolTip UDF BrewManNH | Can't find what you're looking for on the Forum?
JohnOne Posted July 21, 2010 Posted July 21, 2010 As far as I se, those variables are swapped. AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans.
squid808 Posted July 21, 2010 Posted July 21, 2010 Nope, I often use a variable in the same line as I redefine it to no ill effect. For instance, when I'm working with names I can do something like the following: $name="squid808" $name=stringtrimright($name,stringlen($name)-2) msgbox(0,"Hi","The first two letters of my SN are " & $name) You don't have to worry about having a temporary or swap variable to hold on to things.
jaberwacky Posted July 21, 2010 Author Posted July 21, 2010 I'm thinking behind the scenes here like what would the compiler do? ... wwcd ... Helpful Posts and Websites: AutoIt3 Variables and Function Parameters MHz | AutoIt Wiki | Using the GUIToolTip UDF BrewManNH | Can't find what you're looking for on the Forum?
omikron48 Posted July 21, 2010 Posted July 21, 2010 (edited) Those variables are memory locations. What happens is, when you perform a calculation, the instruction for the operation is loaded into the CPU with all its flags and registers. After the calculation is performed, the result is then stored into whatever memory location is referenced by the variable. So in essence, the CPU will be temporarily holding on to the result so there's nothing stopping you from storing the result in the same memory location as one of the parameters used for the calculation. Edited July 21, 2010 by omikron48
ShawnW Posted July 22, 2010 Posted July 22, 2010 I'm thinking behind the scenes here like what would the compiler do? ... wwcd ...There is no compiler. This is an interpreted language. The .exe is an illusion just containing the source and interpreter with instructions on what to do.
jaberwacky Posted July 22, 2010 Author Posted July 22, 2010 Those variables are memory locations. What happens is, when you perform a calculation, the instruction for the operation is loaded into the CPU with all its flags and registers. After the calculation is performed, the result is then stored into whatever memory location is referenced by the variable.So in essence, the CPU will be temporarily holding on to the result so there's nothing stopping you from storing the result in the same memory location as one of the parameters used for the calculation.Ok, so it would take two or three registers?There is no compiler. This is an interpreted language. The .exe is an illusion just containing the source and interpreter with instructions on what to do.I never knew that! thanks Helpful Posts and Websites: AutoIt3 Variables and Function Parameters MHz | AutoIt Wiki | Using the GUIToolTip UDF BrewManNH | Can't find what you're looking for on the Forum?
Richard Robertson Posted July 22, 2010 Posted July 22, 2010 Another way to switch (works on integers, which includes characters and pointers). A XOR EQUALS B B XOR EQUALS A A XOR EQUALS B The values are now swapped.
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