Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong compilation result on list indexing operation #55

Open
m13253 opened this issue Aug 21, 2016 · 0 comments
Open

Wrong compilation result on list indexing operation #55

m13253 opened this issue Aug 21, 2016 · 0 comments

Comments

@m13253
Copy link

m13253 commented Aug 21, 2016

Demo source code:

array = [0, 1, 2, 3];
i = 2;

// Wrong
array[i] = 42;
println("array[i] = " ++ array[i]);
println("array = [" ++ array[0] ++ ", " ++ array[1] ++ ", " ++ array[2] ++ ", " ++ array[3] ++ "]");

println();

// Correct
batch("set /a array_!i!=42");
batch("for %%i in (\"!i!\") do echo array[i] = !array_%%~i!");
println("array = [" ++ array[0] ++ ", " ++ array[1] ++ ", " ++ array[2] ++ ", " ++ array[3] ++ "]");

Compiled results:

@echo off
setlocal EnableDelayedExpansion
setlocal EnableExtensions

set /a array_0=0
set /a array_1=1
set /a array_2=2
set /a array_3=3
set /a i=2
rem Wrong
set /a array_i=42
echo array[i] ^= !array_i!
echo array ^= [!array_0!^, !array_1!^, !array_2!^, !array_3!]
echo:
rem Correct
set /a array_!i!=42
for %%i in ("!i!") do echo array[i] = !array_%%~i!
echo array ^= [!array_0!^, !array_1!^, !array_2!^, !array_3!]

Execution output:

array[i] = 42
array = [0, 1, 2, 3]

array[i] = 42
array = [0, 1, 42, 3]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant