如何跨 ENDLOCAL 返回值数组 [英] How to return an array of values across ENDLOCAL

查看:12
本文介绍了如何跨 ENDLOCAL 返回值数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个可变长度的值数组,TargetName[] 和 TargetCpu[],我需要返回 ENDLOCAL 边界.我尝试了以下方法,但只返回第一个数组上的第一个值.

I have two variable length arrays of values, TargetName[] and TargetCpu[], which I need to return across the ENDLOCAL boundary. I've tried the following, but only the first value on the first array gets returned.

for /L %%i in (0,1,%MaxIndex%) do (
    for /f "delims=" %%j in (""!TargetName[%%i]!"") do (
        for /f "delims=" %%k in (""!TargetCpu[%%i]!"") do (
            endlocal
            set TargetName[%%i]=%%j
            set TargetCpu[%%i]=%%k
        )
    )
)

下面是返回值的打印.

Number Targets: 3
TargetName[0]: "Computer1"
TargetCpu[0] : "x64"
TargetName[1]: "!TargetName[1]!"
TargetCpu[1] : "!TargetCpu[1]!"
TargetName[2]: "!TargetName[2]!"
TargetCpu[2] : "!TargetCpu[2]!"

我已经阅读了我能找到的所有内容,但我尝试过的所有内容都不适用于可变长度数组.

I've read about everything I can find, but nothing I've tried works for a variable length array.

推荐答案

@echo off
setlocal

set "MaxIndex=6"
call :CreateArrays
set TargetName
set TargetCPU
goto :EOF


:CreateArrays

setlocal EnableDelayedExpansion
for /L %%i in (1,1,%MaxIndex%) do (
   set /A TargetName[%%i]=!random!, TargetCpu[%%i]=!random!
)

rem Return the arrays to the calling scope
set "currentScope=1"
for /F "delims=" %%a in ('set TargetName[ ^& set TargetCPU[') do (
   if defined currentScope endlocal
   set "%%a"
)
exit /B

这篇关于如何跨 ENDLOCAL 返回值数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆