China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-06-30 02:28
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » [Trick] Swap the values of two variables without using a temporary variable View 8,156 Replies 20
Original Poster Posted 2007-01-25 08:16 ·  中国 广东 电信
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
General type:

@echo off
set var1=abc
set var2=123
echo Before exchange: var1=%var1% var2=%var2%
set var1=%var2%& set var2=%var1%
echo After exchange: var1=%var1% var2=%var2%
pause

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

Scheme one:

@echo off
set /a num1=123,num2=456
echo Before exchange: num1=%num1% num2=%num2%
set /a num1=%num2%,num2=%num1%
:: set num1=%num2%& set num2=%num1% is also acceptable
echo After exchange: num1=%num1% num2=%num2%
pause


Scheme two:

@echo off
:: There will be a limit on the value range, that is, the sum of num_a and num_b must be within the range of 2^-31 to 2^31-1
set /a num_a=123,num_b=456
echo Before exchange: num_a=%num_a% num_b=%num_b%
set /a num_a=%num_a%+%num_b%-%num_a%,num_b=%num_a%+%num_b%-%num_b%
echo After exchange: num_a=%num_a% num_b=%num_b%
pause
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 2 Posted 2007-01-25 08:27 ·  美国 弗吉尼亚州 华盛顿县 Microsoft
高级用户
★★
Credits 783
Posts 268
Joined 2006-12-26 17:18
19-year member
UID 74627
Gender Male
Status Offline
Record it for future use!~
菩提本无树,明镜亦非台,本来无一物,何处惹尘埃.
Floor 3 Posted 2007-01-25 08:29 ·  中国 广东 广州 电信
初级用户
Credits 99
Posts 43
Joined 2007-01-12 23:05
19-year member
UID 76317
Gender Male
Status Offline
Hehe, tried it, really, ... still thinking about it...
我很菜,但我很努力~
Floor 4 Posted 2007-01-25 09:02 ·  中国 北京 联通
中级用户
★★
带走
Credits 435
Posts 88
Joined 2005-09-24 19:22
20-year member
UID 42793
Status Offline
Not bad, making clever use of the feature of environment variable expansion. Actually, these three examples should all be of the same meaning, right?
Floor 5 Posted 2007-01-25 10:11 ·  中国 广东 电信
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
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.
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 6 Posted 2007-01-25 10:25 ·  中国 湖南 娄底 新化县 电信
银牌会员
★★★
Credits 1,218
Posts 485
Joined 2006-07-21 21:24
19-year member
UID 58987
From 湖南.娄底
Status Offline

The algorithm to swap the values of two variables is very simple.

Suppose there are two cups: A and B. To swap the water in cups A and B, a third cup C is needed.
1. Pour the water from cup A into cup C;
2. Pour the water from cup B into cup A;
3. Pour the water from cup C into cup A;

In this way, the water in the two cups is swapped.


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.



set x=2
set y=3
set x=%y%&set y=%x%

Here, the last line is macro-expanded to set x=3&set y=2, and the values of x and y are automatically expanded into the assignment statement, so the values of the variables are swapped without an intermediate variable. Here, the interpreter acts as the intermediate variable.


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?
业精于勤而荒于嬉,形成于思而毁于随。
Floor 7 Posted 2007-01-25 10:41 ·  中国 山西 运城 联通
银牌会员
★★★
天的白色影子
Credits 2,343
Posts 636
Joined 2004-03-06 00:00
22-year member
UID 19350
Gender Male
Status Offline
Teaching Cases of C Language Textbooks


set /a "a=b^a"
set /a "b=b^a"
set /a "a=b^a"


In addition, four arithmetic operations can also be implemented

Just not as flexible and unrestricted as XOR operation


set /a a=a+b
set /a b=a-b
set /a a=a-b
Floor 8 Posted 2007-01-25 10:46 ·  中国 湖南 娄底 新化县 电信
银牌会员
★★★
Credits 1,218
Posts 485
Joined 2006-07-21 21:24
19-year member
UID 58987
From 湖南.娄底
Status Offline
Yes, in terms of both efficiency and scope of use, the four arithmetic operations are not as good as bitwise operations.


@echo off
set a=2
set b=3
echo a=%a% b=%b%
set /a "a=b^a"
set /a "b=b^a"
set /a "a=b^a"
echo a=%a% b=%b%
set /a a=a+b
set /a b=a-b
set /a a=a-b
echo a=%a% b=%b%
pause
业精于勤而荒于嬉,形成于思而毁于随。
Floor 9 Posted 2007-01-25 10:59 ·  中国 北京 联通
中级用户
★★
带走
Credits 435
Posts 88
Joined 2005-09-24 19:22
20-year member
UID 42793
Status Offline
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...
Floor 10 Posted 2007-01-25 11:07 ·  中国 浙江 宁波 北仑区 电信
新手上路
Credits 1
Posts 2
Joined 2006-12-31 06:23
19-year member
UID 75076
Gender Male
Status Offline
Floor 11 Posted 2007-01-25 11:17 ·  中国 广东 电信
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
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.
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 12 Posted 2007-01-25 11:20 ·  中国 湖南 娄底 新化县 电信
银牌会员
★★★
Credits 1,218
Posts 485
Joined 2006-07-21 21:24
19-year member
UID 58987
From 湖南.娄底
Status Offline
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.
业精于勤而荒于嬉,形成于思而毁于随。
Floor 13 Posted 2007-01-25 13:49 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
19-year member
UID 59080
Status Offline

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.
@echo off
set var1=abc
set var2=123
echo Before exchange: var1=%var1% var2=%var2%
set var1=%var2%&call set var2=%%var1%%
echo After exchange: var1=%var1% var2=%var2%
pause
Floor 14 Posted 2007-01-26 12:03 ·  中国 湖南 娄底 新化县 电信
银牌会员
★★★
Credits 1,218
Posts 485
Joined 2006-07-21 21:24
19-year member
UID 58987
From 湖南.娄底
Status Offline
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.
业精于勤而荒于嬉,形成于思而毁于随。
Floor 15 Posted 2007-01-27 05:33 ·  中国 安徽 芜湖 电信
高级用户
★★★
Credits 866
Posts 415
Joined 2005-12-04 11:19
20-year member
UID 46459
Status Offline
Forum Jump: