我试图比较9个数字的序列(以 ,
)使用一个批处理文件,比较总是通过相应的序列来进行,如。
mPrevious[0] <-> mCurrent[0]
mPrevious[1] <-> mCurrent[1]
我需要知道是否至少有一个序列发生了变化。在下面的例子中,我需要知道至少有一个序列发生了变化。234
改为 230
和 146
到 149
.
我目前的草图是。
setlocal ENABLEDELAYEDEXPANSION
@echo off
set mPrevious=229,234,235,127,58,0,131,133,146
set mCurrent=229,230,235,127,58,0,131,133,149
for /f "tokens=1,2,3,4,5,6,7,8,9 delims=," %%a IN ('echo !mPrevious!') do (
)
参赛作品的数量(目前是9个)将来可能会改变。但目前只是9个。
我不知道在一个批处理脚本中正确的方法是什么。
该 FOR
token delimiters是。<SPACE> <TAB> <NBSP> , ; =
因此,你可以把它放入 FOR
循环,但如果内容包含了 *
或 ?
.
@echo off
====SETLOCAL EnableDelayedExpansion EnableExtensions
set/a"#=cnt=0"
::Define lists
set "mPrevious=229,234,235,127,58,0,131,133,146"
set "mCurrent=229,230,235,127,58,0,131,133,149"
FOR %%P in (!mPrevious!) do (
FOR %%C in (!mCurrent!) do (
if !cnt! equ !#! echo(%%P %%C
set/a"cnt+=1"
)
set/a"cnt=0,#+=1"
)
@echo off
title <nul && title ...\%~nx0
setlocal enabledelayedexpansion
set "_mPrevious=229,234,235,127,58,0,131,133,146"
set "_mCurrents=229,230,235,127,58,0,131,133,149"
echo/!_mPrevious!|find "!_mCurrents!" >nul && (
endlocal & echo\Nothing changed^!! & goto :EOF )
for %%i in (!_mPrevious!)do set /a "_i+=1+0" && call set "_mPrev_!_i!=%%~i"
for %%j in (!_mCurrents!)do set /a "_j+=1+0" && call set "_mCurr_!_j!=%%~j"
if !_i! neq !_j! endlocal & echo\Varyables have different lengths^!! & goto :EOF
for /L %%L in (1 1 !_j!)do if !_mPrev_%%~L! neq !_mCurr_%%~L! echo\!_mPrev_%%~L! updated to: !_mCurr_%%~L!
endlocal && goto :EOF
234 updated to: 230
146 updated to: 149
一个简单的方法,只有在必要的情况下,只有当两个变量具有相同的长度。
进行第一次比较 如果变量相同,有一个变化的值。
echo/!_mPrevious!|find "!_mCurrents!" >nul && (
endlocal & echo\Nothing changed^!! & goto :EOF )
如果两个变量的长度相同,则进行第二次比较。
if !_i! neq !_j! endlocal & echo\Variables have different lengths^!! & goto :EOF
观察..: 1. 我更喜欢用替换法 [ ]
到一个简单的 _
观察: 2. 另外,改变 i+=
到 _i+=1+0
,其中无需预定义 set
命令。set i=0
这是一种使用一些自扩展代码的方法。
@echo off
setlocal EnableDelayedExpansion
rem // Define constants here:
set "mPrevious=229,234,235,127,58,0,131,133,146"
set "mCurrents=229,230,235,127,58,0,131,133,149"
rem // Initialise auxiliary variables and indexes:
set "nPrevious=,%mPrevious%" & set /A "i=0"
set "nCurrents=,%mCurrents%" & set /A "j=0"
rem // Convert lists to arrays using self-expanding code:
set "_=%nPrevious:,=" & set /A "i+=1" & set "nPrevious[!i!]=%"
set "_=%nCurrents:,=" & set /A "j+=1" & set "nCurrents[!j!]=%"
rem // Verify availability of arrays:
> nul 2>&1 set nPrevious[ || set /A "i=0"
> nul 2>&1 set nCurrents[ || set /A "j=0"
rem // Determine minimal and maximal count:
if %j% gtr %i% (set /A "k=i, l=j" & set "_=#") else (set /A "k=j, l=i" & set "_=")
rem // Compare corresponding elements:
for /L %%K in (1,1,%k%) do if !nPrevious[%%K]! neq !nCurrents[%%K]! (
echo [%%K]: !nPrevious[%%K]! -^> !nCurrents[%%K]!
)
rem // Return removed or added elements:
set /A "k+=1" & for /L %%K in (!k!,1,%l%) do if defined _ (
echo [%%K]: --- -^> !nCurrents[%%K]!
) else (
echo [%%K]: !nPrevious[%%K]! -^> ---
)
endlocal
输出样本,依靠问题的数据。
[2]: 234 -> 230 [9]: 146 -> 149