中国DOS联盟论坛

China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --
Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
Guest | Log in | Register | Members | Search | China DOS Union
中国DOS联盟论坛
The time now is 2026-08-14 04:00
47,811 topics / 349,897 posts / today 0 new / 48,256 members
DOS批处理 & 脚本技术(批处理室) » [Trick] Swap the values of two variables without using a temporary variable
Printable Version  8,510 / 20
Floor1 namejm Posted 2007-01-25 08:16
荣誉版主 Posts 1,737 Credits 5,226 From 成都
General type:

If the values of the two variables to be exchanged are numbers, the following two schemes can also be used:

Scheme one:


Scheme two:
Floor2 PPdos Posted 2007-01-25 08:27
高级用户 Posts 268 Credits 783
Record it for future use!~
Floor3 hangyug Posted 2007-01-25 08:29
初级用户 Posts 43 Credits 99
Hehe, tried it, really, ... still thinking about it...
Floor4 0401 Posted 2007-01-25 09:02
中级用户 Posts 88 Credits 435
Not bad, making clever use of the feature of environment variable expansion. Actually, these three examples should all be of the same meaning, right?
Floor5 namejm Posted 2007-01-25 10:11
荣誉版主 Posts 1,737 Credits 5,226 From 成都
Originally posted by 0401 at 2007-1-24 20:02:
Not bad, cleverly using the characteristics of environment variable expansion. In fact, these three examples should all be of the same meaning, right?

  If you have carefully looked at the code, you will feel that they are not the same idea: the universal code uses & to exchange the values of two variables. Solution 1 uses some characteristics of set /a. On the basis of using the characteristics of set /a, Solution 2 also uses the formula A+B-A=B, A+B-B=A to exchange the values of variables.

  In fact, these几段demonstration codes also lead to a relatively interesting topic: set var1=%var2% has assigned the value of var2 to var1. According to reason, at this time the value of var1 has changed, but when using & to lead to another assignment statement set var2=%var1%, why can var2 still get the value before var1 changed? Does the variable var1 can store more than one value at the same time? set /a num1=num2%,num2=%num1% also has a similar phenomenon.
Floor6 pengfei Posted 2007-01-25 10:25
银牌会员 Posts 485 Credits 1,218 From 湖南.娄底


Among the three methods given by brother namejm, the first one uses the pipe character to swap variables, and the latter two are similar in meaning, using the comma operator to swap variables.

In fact, this method is realized by the particularity of the script. If you are familiar with macros in the C language, it is not difficult to understand. The script is executed by the interpreter. When the batch script interpreter is interpreting, it must first perform macro expansion, and in general, it is interpreted line by line.



In fact, there are also methods to swap the values of two variables without using an intermediate variable in binary programs.

That is to use bit operations to achieve it, hehe~ Everyone think about it?
Floor7 qzwqzw Posted 2007-01-25 10:41
银牌会员 Posts 636 Credits 2,343
Teaching Cases of C Language Textbooks



In addition, four arithmetic operations can also be implemented

Just not as flexible and unrestricted as XOR operation

Floor8 pengfei Posted 2007-01-25 10:46
银牌会员 Posts 485 Credits 1,218 From 湖南.娄底
Yes, in terms of both efficiency and scope of use, the four arithmetic operations are not as good as bitwise operations.

Floor9 0401 Posted 2007-01-25 10:59
中级用户 Posts 88 Credits 435
In a single statement, the assignment of a variable cannot be expanded (referenced) immediately; it can only be expanded in the next statement. A single statement refers to if, for statements, and statements connected using parentheses && & ||, etc. Unless environment variable expansion delay is enabled and "!" is used instead of "%" to expand the variable value. So that's why I said that Brother namejm's three examples are similar in meaning...
Floor10 simazhuo Posted 2007-01-25 11:07
新手上路 Posts 2 Credits 1
Floor11 namejm Posted 2007-01-25 11:17
荣誉版主 Posts 1,737 Credits 5,226 From 成都
Originally posted by pengfei at 2007-1-24 21:25:Scripts are executed by an interpreter. When a batch script is interpreted, macro expansion must be performed first, and in general, it is interpreted line by line.

  Hehe, reminds me of this is the preprocessing mechanism of batch processing ^_^, the accurate statement seems to be executed line by line.
Floor12 pengfei Posted 2007-01-25 11:20
银牌会员 Posts 485 Credits 1,218 From 湖南.娄底
Yes, generally speaking, it is explained line by line. It should be changed to executed sentence by sentence. For compound statements in parentheses, the interpreter also executes them at one time.
Floor13 lxmxn Posted 2007-01-25 13:49
版主 Posts 4,938 Credits 11,386

It feels that what user 0401 on floor 9 said is relatively reasonable. If variable delay is enabled, some of the above examples may not work.
Floor14 pengfei Posted 2007-01-26 12:03
银牌会员 Posts 485 Credits 1,218 From 湖南.娄底
Brother lxmxn, using call %%var1%% doesn't actually expand, just like !var!, which is another way of variable delay. Therefore, variable swapping can't be achieved either.
Floor15 htysm Posted 2007-01-27 05:33
高级用户 Posts 415 Credits 866
1 2  Next
[ Contact the Union admin team - 中国DOS联盟 - Standard version ]
Sponsored by ifanr Inc | © 2001–2023