批处理模拟一维数组

代码如下:

@echo off
setlocal ENABLEDELAYEDEXPANSION

set tmp="30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1"
set tmp1="10 9 8 7 6 5 4 3 2 1"

call:fun_arr %tmp% array len
for /l %%i in (1,1,%len%) do (
	echo array[%%i]=!array[%%i]!
)
echo --
call:fun_arr %tmp1% array len
for /l %%i in (1,1,%len%) do (
	echo array[%%i]=!array[%%i]!
)

pause
goto:eof

:: ///////////////////////////////////////////
:fun_arr
	rem 用法:call:fun_arr %tmp% array len
	rem  in: %tmp%  - 空格分隔的数据
	rem out: array  - 数组名
	rem      len    - 数组长度

	set tmpstr=%1
	set tmpstr=%tmpstr:"=%
	set "arr=%2"

	rem 清空数组
	set /a n=0
	:fun_arrCls_loop
		set /a n+=1
		if "!%arr%[%n%]!" equ "" (goto:fun_arr_continue)
		set "%arr%[!n!]="
	goto:fun_arrCls_loop

	rem 数组赋值
	:fun_arr_continue
	set /a n=0
	for %%j in (%tmpstr%) do (
		set /a n+=1
		set %arr%[!n!]=%%j
	)
	set %3=%n%
goto:eof


猜你喜欢

转载自blog.csdn.net/end1n9/article/details/28104795