0

I have a below code and I cannot get the timeout: !timeout[%i%]! value displayed. It should be 120.

@echo off

setlocal enableextensions enabledelayedexpansion

set /A "x = 0"
set /A "timeout[1] = 120"
set /A "timeout[2] = 130"

if !x! equ 0 (
set /A "i = !x! + 1"
echo x: !x!, i: !i!, timeout: !timeout[%i%]!
pause

)
endlocal

Already tried %timeout[%i%]%, !timeout[!i!]!

aschipfl
  • 31,767
  • 12
  • 51
  • 89
user11702680
  • 101
  • 1
  • 1
    [Stephan's DELAYEDEXPANSION link](https://stackoverflow.com/a/30284028/2128947) . Since you are using `set /a`, quotes are unnecessary. Also you can use a simple `set /A i = x + 1` and `set/a` will use the run-time value of `x`. In the `echo` statement, you need `%timeout[!i!]%` as `i` *has* changed in the code block, but `timeout...` has not. – Magoo Jun 01 '22 at 17:20
  • 1
    `%timeout[!i!]%` as suggested by @Magoo does not work here too, `%timeout[!i!]%` is replaced already by no string before the `if` condition is executed at all. `%i%` in `!timeout[%i%]!` is replaced already by nothing before executing the `if` condition resulting in `echo` command line ending with `!timeout[]!`. – Mofi Jun 01 '22 at 17:42
  • 2
    Yes - @mofi is quite correct. Have to stop doing this when I'm tired... I meant `CALL echo x: !x!, i: !i!, timeout: %%timeout[!i!]%%` – Magoo Jun 01 '22 at 17:52
  • [Arrays, linked lists and other data structures in cmd.exe (batch) script](https://stackoverflow.com/a/10167990) – aschipfl Jun 01 '22 at 21:04

0 Answers0