比较批处理脚本中的数字数组

I'm trying to compare a sequence of 9 numbers (separated by ,) using a batch file. The comparison is always made by the corresponding sequence like:

mPrevious[0] <-> mCurrent[0]
mPrevious[1] <-> mCurrent[1]

I need to know if at least one sequece have changed. In the example bellow, 234 changed to 230 and 146 to 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岁。

我不确定在批处理脚本中执行此操作的正确方法是什么。

评论
带我飞
带我飞

让我分享我的方法,并且我不是该方法的作者。 这是在cmd / bat中定义/使用数组的安全方法。

I saw this on dostips.com /by @Aacini and after here /by @user6811411 (removed user), some time ago. The original layout code is some like:

Set "var=%var:,="&Set /a i+=1&Set "var[!i!]=%

I prefer replace [ ] to one simple _

Also, change i+= to _i+=1+0, where no need predefined set command: set i=0

@echo off && setlocal enabledelayedexpansion

title <nul && title ...\%~nx0

set "_mPrevious=229,234,235,127,58,0,131,133,146"
set "_mCurrents=229,230,235,127,58,0,131,133,149"

rem :: replace/incrementing delimiter ',' by a set /a_i+=1+0%% :: 
rem :: (miracle loop in set new variable _var_+i/index) ::

set "_mPrevious_0=%_mPrevious:,="&set/a _i+=1+0&set "_mPrevious_!_i!=%"
set "_mCurrents_0=%_mCurrents:,="&set/a _k+=1+0&set "_mCurrents_!_k!=%"

rem :: use a for loop to echo/use the index array value i/I ::

for /l %%i in (0 1 !_i!)do if "!_mPrevious_%%i!" neq "!_mCurrents_%%i!" (
   echo="!_mPrevious_%%i!" updated to: "!_mCurrents_%%i!"
   rem./ And if needed: && set "_mPrevious_%%i=!_mCurrents_%%i!" 
   )

endlocal && goto :EOF

输出结果:

"234" updated to: "230"
"146" updated to: "149"

There is also another answer about this method here:

点赞
评论