批处理模拟一维数组

代码如下:

@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


  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值