将赋值数组值分配给变量

问题描述 投票:0回答:2

我有此代码:

@echo off
SET /a counter=0
SET _inputname=
Set interfejs=ala
FOR /f "skip=3 tokens=*" %%a in ('netsh int ip show interfaces') do ( call :myfunct "%%a" )

SET /P _inputname=interface: 
IF "%_inputname%" LEQ "%counter%" IF "%_inputname%" GTR "0" (
echo variable value:%_inputname%
call echo [%_inputname%] %%NetInterfNames[%_inputname%]%%

rem problem with assigning array value to a variable.

Set interfejs=%%NetInterfNames[%_inputname%]%%
echo to jest wybrany interface: %interfejs%

Rem split string
rem for /F "tokens=1,*" %%a in ("hello how are you") do echo %%b

)   
IF "%_inputname%" GTR "%counter%" ( GOTO :EOF )
IF "%_inputname%" EQU "0" ( GOTO :EOF )

    goto: EOF

:myfunct
set /a counter=%counter%+1
set NetInterfNames[%counter%]=%~1
call echo [%counter%] %%NetInterfNames[%counter%]%%

我正在尝试使用此方法将从数组中获取的值分配给变量

Set interfejs = %% NetInterfNames [%_ inputname%] %%

问题是未分配值形式数组。有没有人知道如何处理?

arrays batch-file variables assign
2个回答
0
投票

如果您enabledelayedexpansion,它将帮助您轻松实现这一目标。

@echo off
setlocal enabledelayedexpansion
SET /a counter=0
SET _inputname=
Set interfejs=ala
FOR /f "skip=3 tokens=*" %%a in ('netsh int ip show interfaces') do call :myfunct "%%~a"

SET /P _inputname=interface: 
IF %_inputname% LEQ %counter% IF %_inputname% GTR 0 (
echo variable value:%_inputname%
echo [%_inputname%] !NetInterfNames[%_inputname%]!

Set interfejs=!NetInterfNames[%_inputname%]!
echo to jest wybrany interface: !interfejs!

)   
IF %_inputname% GTR %counter% GOTO :EOF
IF %_inputname% EQU 0 GOTO :EOF

    goto :eof

:myfunct
set /a counter+1
set NetInterfNames[%counter%]=%~1
call echo [%counter%] !NetInterfNames[%counter%]!

0
投票

这里没有启用延迟扩展,根据我的评论建议,向您显示您需要使用附加的Call((我添加了Pause以允许您查看任何输出)

@Echo Off
Set "counter=0"
Set "_inputname="
Set "interfejs=ala"
For /F "Skip=3 Tokens=*" %%A In ('NetSh int ip show interfaces') Do Call :myfunct "%%A"

Set /P "_inputname=interface: "
If "%_inputname%" LEq "%counter%" If "%_inputname%" Gtr "0" (
    Echo variable value:%_inputname%
    Call Echo [%_inputname%] %%NetInterfNames[%_inputname%]%%

    Rem no problem with assigning array value to a variable.

    Call Set "interfejs=%%NetInterfNames[%_inputname%]%%"
    Call Echo to jest wybrany interface: %%interfejs%%

    Rem split string
    Rem For /F "Tokens=1*" %%A In ("hello how are you") Do Echo %%B

)
If "%_inputname%" Gtr "%counter%" GoTo :EOF
If "%_inputname%" Equ "0" GoTo :EOF

Pause&GoTo :EOF

:myfunct
Set /A counter+=1
Set "NetInterfNames[%counter%]=%~1"
Call Echo [%counter%] %%NetInterfNames[%counter%]%%

但是,如果您移动两个If命令,则完全可以不需要其中一个代码块:

@Echo Off
Set "interfejs=ala"

Set "counter=0"
For /F "Skip=3 Tokens=*" %%A In ('NetSh int ip show interfaces') Do Call :myfunct "%%A"

Set "_inputname="
Set /P "_inputname=interface: "
If Not Defined _inputname GoTo :EOF
If "%_inputname%" Gtr "%counter%" GoTo :EOF
If "%_inputname%" Equ "0" GoTo :EOF
Echo variable value:%_inputname%
Call Echo [%_inputname%] %%NetInterfNames[%_inputname%]%%
Call Set "interfejs=%%NetInterfNames[%_inputname%]%%"
Echo to jest wybrany interface: %interfejs%

Rem split string
Rem For /F "Tokens=1*" %%A In ("hello how are you") Do Echo %%B

Pause&GoTo :EOF

:myfunct
Set /A counter+=1
Set "NetInterfNames[%counter%]=%~1"
Call Echo [%counter%] %%NetInterfNames[%counter%]%%

您可能还可以在不启用延迟扩展的情况下进行一些改进:

@Echo Off
For /F "Delims==" %%A In ('Set NetInterfNames[ 2^>NUL')Do Set "%%A="
For /F "Tokens=1* Delims=:" %%A In (
    'NetSh int ip show interfaces 2^>NUL^|Findstr "[0-9]"^|FindStr /N "^"'
)Do Set "NetInterfNames[%%A]=%%B"&Call Echo [%%A] %%NetInterfNames[%%A]%%
:Input
Set /P "_inputname=interface: "
If Not Defined NetInterfNames[%_inputname%] GoTo Input
Call Set "_interfejs=%%NetInterfNames[%_inputname%]%%
Echo to jest wybrany interface: "%_interfejs%"

Rem split string
Rem For /F "Tokens=1*" %%A In ("hello how are you") Do Echo %%B

Pause&GoTo :EOF
© www.soinside.com 2019 - 2024. All rights reserved.