我已经写了一个脚本来蝙蝠文件,将打印列表中的随机值。但我面临使用索引访问列表值的问题
我的代码是:
set list=A B C D a b c
echo %list[3]%
for /l %%a in (1,1,6) do (
@set /a bottomlimit = 0
@set /a upperlimit = 5
@set /a num = !bottomlimit! + !RANDOM! %% !upperlimit! - !bottomlimit! + 1
echo %list[!num!]%
TIMEOUT /T 5
)
等待您有价值的解决方案。
只是三个选择。处理您的方法的第一要务。环境变量中“纯”数组的第二个。第三个将混合使用列表1中定义的列表,但在列表2中迭代列表以生成数组。
@echo off
setlocal enableextensions enabledelayedexpansion
REM OPTION 1 - The list
echo -------------------------------------------------
setlocal
set "list=A B C D a b c"
set /a bottomlimit=0
set /a upperlimit=6
for /l %%a in (1,1,6) do (
set /a "num=!bottomlimit! + ( !RANDOM! %% (!upperlimit! - !bottomlimit! + 1))"
set "pos=0"
for %%l in (!list!) do if defined pos if !pos!==!num! ( echo %%l & set "pos=" ) else ( set /a "pos+=1")
)
endlocal
REM OPTION 2 - The "pure" array
echo -------------------------------------------------
setlocal
set "list[0]=A"
set "list[1]=B"
set "list[2]=C"
set "list[3]=D"
set "list[4]=a"
set "list[5]=b"
set "list[6]=c"
set /a bottomlimit=0
set /a upperlimit=6
for /l %%a in (1,1,6) do (
set /a "num=!bottomlimit! + ( !RANDOM! %% (!upperlimit! - !bottomlimit! + 1))"
for %%n in (!num!) do echo !list[%%n]!
)
endlocal
REM OPTION 3 - The remix
echo -------------------------------------------------
setlocal
set "list=A B C D a b c"
set "pos=0"
for %%l in (!list!) do ( set "list[!pos!]=%%l" & set /a "pos+=1" )
set /a "bottomlimit=0"
set /a "upperlimit=!pos!-1"
for /l %%a in (1,1,6) do (
set /a "num=!bottomlimit! + ( !RANDOM! %% (!upperlimit! - !bottomlimit! + 1))"
for %%n in (!num!) do echo !list[%%n]!
)
endlocal
endlocal
exit /b
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句