1

I'm trying to set a variable with the value from an array in a batch file, but no solution seems to work so far. Here's a snippet of the situation:

:: random value
set ParId=123

:: get the last number of the previous value
set /a "temp0=%ParId% %% 10"

:: part of my array, just for the example
set AsciiNumTable[0]=0x30
set AsciiNumTable[1]=0x31
set AsciiNumTable[2]=0x32
set AsciiNumTable[3]=0x33
set AsciiNumTable[4]=0x34
set AsciiNumTable[5]=0x35
set AsciiNumTable[6]=0x36
set AsciiNumTable[7]=0x37
set AsciiNumTable[8]=0x38
set AsciiNumTable[9]=0x39

:: PART NOT WORKING
set ParIdByte0=AsciiNumTable[%temp0%]

I've tried different methods:

call set ParIdByte0=%AsciiNumTable[!tempId5!]%

but they also do not work.

If I output the array and the temp variable they look fine, so I don't know why I can't get the value from the array.

g_br
  • 21
  • 6
  • Basically you have to make sure that the "inner" variable (the one holding the index) becomes expanded before the "outer" one (the array variable itself)... – aschipfl Apr 03 '18 at 15:12
  • This management is fully explained at [this answer](https://stackoverflow.com/questions/10166386/arrays-linked-lists-and-other-data-structures-in-cmd-exe-batch-script/10167990#10167990) – Aacini Apr 04 '18 at 18:46

2 Answers2

0

"do not work" is meaningless. Please state what you expect and what actually happened.

Try

call set ParIdByte0=%%AsciiNumTable[%temp0%]%%
Magoo
  • 77,302
  • 8
  • 62
  • 84
  • Hi! The issue was that ParIdByte0 was not receiving any value, but the solution from npocmaka worked fine. I tried your approach, but unfortunately it didn't seem to work. Thanks! – g_br Apr 03 '18 at 15:00
  • "Not work" is still meaningless. Your original code had `temp0` set to `3` but had not set that array value. Did you try this approach in your modified code? – Magoo Apr 04 '18 at 00:55
  • Hi. I think it is pretty clear that ParIdByte0 was not getting any value, as npocmaka noted, hence "not working". The original post had only a rough transcription (which contained mistakes). Using the approach from the other answer solved the issue. Thanks. – g_br Apr 04 '18 at 08:37
0

It can be done in a reverse approach you are trying (with using delayed expansion).THough there are few more issues with your script 1)Missing set keyword when assigning value to ParId 2) temp0 value will be 3 and you have no third element in the array:

@echo off
setlocal enableDelayedExpansion
:: random value
set ParId=123

:: get the last number of the previous value
set /a "temp0=%ParId% %% 10"

:: part of my array, just for the example
set AsciiNumTable[0]=0x30
set AsciiNumTable[1]=0x31
set AsciiNumTable[2]=0x32
set AsciiNumTable[3]=0x33

:: PART NOT WORKING
set ParIdByte0=!AsciiNumTable[%temp0%]!

echo --%ParIdByte0%--
npocmaka
  • 55,367
  • 18
  • 148
  • 187
  • Hi! This works just fine. I had tried different combinations for the last 'set', but not the one you suggested. Also, I just tried to quickly replicate the code here, that's why some stuff is missing, I'll improve it so it looks better. Thanks! – g_br Apr 03 '18 at 14:59