0

I would like to call variables that contain other variables in their name when I have enabledelayedexpension so I would have concentric exclamation points in my variable call. I apologize for the unclear wording; I'm not very familiar with this subject.

Here's a portion of my code that shows my issue:

set /a Freq[%%z]value[!value!]+=%%y
echo Freq %%z value !value! is !Freq[%%z]value[!value!]!

As is, batch interprets !Freq[%%z]value[!value!]! broken up into different variables with the !'s, and I don't think I can use %'s instead because I'm in a for loop where this variable is changing. I don't think I can use another for loop to replace !value! with a %%a either.

Here's a more complete look at my code:

set /a line=0
set /a goodLine=0
FOR /F "tokens=* delims=" %%x in (%file%) DO (
    set /a line+=1
    echo Line is !line!
    set data[!line!]=%%x
    
    set /a value=0
    set /a checkGood=0
    FOR %%y in (%%x) DO (
        set /a value+=1
        
        if !value!==1 (
            set /a Freq=%%y
            set /a checkFreq=%%y %% 10
            if !checkFreq!==0 (
                set /a checkGood=1
            ) else (echo bad)
        ) else (
            if !checkGood!==1 (
                for /l %%z in (40, 10, 330) do (
                    if !Freq!==%%z (
                      set /a Freq[%%z]value[!value!]+=%%y
                      echo Freq %%z value !value! is !Freq[%%z]value[!value!]!
                      set /a Freq[%%z]quantity[!value!]+=1
                      echo Freq %%z value !value! quantity is !Freq[%%z]quantity[!value!]!
                    )
                )
            ) else (echo checkGood bad)
        )
    )
)
  • [arrays-linked-lists-and-other-data-structures-in-cmd-exe-batch-script](https://stackoverflow.com/questions/10166386/arrays-linked-lists-and-other-data-structures-in-cmd-exe-batch-script/10167990#10167990): _Another way to achieve the previous process is to use an additional FOR command to change the delayed expansion of the index by an equivalent replaceable parameter, and then use the delayed expansion for the array element_: `for %%a in (!value!) do echo Freq %%z value !value! is !Freq[%%z]value[%%a]!` – Aacini Nov 09 '22 at 19:49
  • this does it, thanks! – Connor McCampbell Nov 10 '22 at 17:02

1 Answers1

0
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION

SET /a value=4
FOR /L %%z IN (1,1,5) DO set /a Freq[%%z]value[!value!]=%%z+10

SET Freq
ECHO ========================

FOR %%y IN (2,7) DO FOR /L %%z IN (1,1,5) DO (
 set /a Freq[%%z]value[!value!]+=%%y
 SET Freq[%%z]value[!value!]
 CALL echo Freq %%z value !value! is %%Freq[%%z]value[!value!]%%
)

GOTO :EOF

Or you could use a for/f "tokens=2delims==" %%e in ('set Freq[%%z]value[!value!]') do echo %%e

depends on what you really want to do.

Magoo
  • 77,302
  • 8
  • 62
  • 84
  • I normally just do `for /f "delims=" %%A in ("!value!") do` and then do things with `!Freq[%%z]value[%%A]!`, but I like how `set` would be far more convenient for situations where there are two inner variables that both use delayed expansion. – SomethingDark Nov 09 '22 at 19:51
  • Thanks for the answers. The additional for loop solves my issue and can be used to call the variable into additional math. The several layers of for loops can make it hard to keep my head straight. Can you explain what your line `CALL echo Freq %%z value !value! is %%Freq[%%z]value[!value!]%%` does, and what are the double %'s doing? – Connor McCampbell Nov 10 '22 at 17:00
  • @ConnorMcCampbell: This and other methods to access array elements are described at the [link I gave before](https://stackoverflow.com/questions/10166386/arrays-linked-lists-and-other-data-structures-in-cmd-exe-batch-script/10167990#10167990)... – Aacini Nov 10 '22 at 17:14