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-08-12 11:47
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Why does this batch file end up showing two "one"s? View 829 Replies 2
Original Poster Posted 2010-04-12 00:44 ·  中国 广东 广州 电信
初级用户
★★
Credits 89
Posts 76
Joined 2009-12-13 13:41
16-year member
UID 156499
Gender Male
Status Offline
@echo off
set a=one
set b=two
echo/Before call :swap a b
call :Swap a b
echo/After call 1 :swap a b
call :Swap b a
echo/After call 2 :swap b a
pause
goto :EOF

:Swap
call set a=%%%1%%
call set b=%%%2%%
set "%1=%b%" & set "%2=%a%" & goto :EOF

----------------------------------------------------------------------
Displayed result:
Before call :swap a b
After call 1 :swap a b
After call 2 :swap b a

I really can't figure it out, please explain..
Floor 2 Posted 2010-04-12 03:24 ·  中国 吉林 延边朝鲜族自治州 延吉市 电信
银牌会员
★★★
正在学习中的菜鸟...
Credits 1,039
Posts 897
Joined 2009-03-01 15:34
17-year member
UID 140302
Gender Male
From 在地狱中仰望天堂
Status Offline
When it gets to echo/After call 2 :swap b a
call set a=%%%1%%
call set b=%%%2%%
because %1=b, %2=a
that is,
set a=!b!
set b=!a!
so as far as the values are concerned, !a!=!b!=one

[ Last edited by Hanyeguxing on 2010-4-12 at 14:04 ]
Floor 3 Posted 2010-04-12 17:18 ·  中国 广东 广州 电信
初级用户
★★
Credits 89
Posts 76
Joined 2009-12-13 13:41
16-year member
UID 156499
Gender Male
Status Offline
Your reply again, thanks first of all!!

Oh, so that's how it is!!

I was treating the variables brought in by CALL as values and doing call set on them, no wonder two disappeared... uh
Forum Jump: