Recalling a variable which value is the name of another variable

Viewed 45

I have written a script to pull specific event data from .csv logs.

2021_06_04 09:00.00, online, Serial#001, 50.10.10.10
2021_06_04 09:01.00, online, Serial#002, 50.10.10.11 
2021_06_04 09:01.00, read, Serial#002, apples, clear 
2021_06_04 09:01.00, online, Serial#003, 50.10.10.12 
2021_06_04 09:01.00, offline, Serial#001 
2021_06_04 09:02.00, read, Serial#002, bananas, clear 
2021_06_04 09:03.00, read, Serial#003, apples, reject 
2021_06_04 09:04.00, read, Serial#002, apples, clear
2021_06_04 09:05.00, read, Serial#003, apples, clear

First I find all machines that came online and store their IP addresses to variables named after their Serial Numbers

for /f "delims=, tokens=3,4" %%a in (
    'type "Log.csv"^|findstr "online"'
) do (
    set %%a=%%b

Now this has assigned variables as expected and as follows

%Serial001% = 50.10.10.10
%Serial002% = 50.10.10.11
%Serial003% = 50.10.10.12

Now I want to find each "read" event per machine. Number of events are counted to create a "variable set"

set /p Search=Search: 

set EventCount = 0
for /f "delims=, tokens=1,3,5" %%a in (
    'type "Log.csv"^|findstr "read"^|findstr %search%'
) do (
    set /a EventCount+=1 
    call set EventTime[%%EventCount%%]=%%a 
    call set Machine[%%EventCount%%]=%%b
    call set Decision[%%BagCount%%]=%%c
)
set TotalCount=%EventCount%

Now I can display each data set, using 'choice' to scroll through the event count

THE MILLION DOLLAR QUESTION!

On the second line I want to add the Machine IP address which was stored in a variable, named after the machine...

%%Machine%% is outputting %Serial#002%

echo. Search:         %search%
echo. Machine:        !Machine[%EventCount%]!    on   %%Machine%%
echo  Decision:       !Decision[%BagCount%]!   @   !EventTime[%EventCount%]!
echo. 
echo. %EventCount%  of  %TotalCount%

expected output

Search:         Apples 
Machine:        Serial#002    on    50.10.10.10
Decision:       Clear   @   2021_06_04 09:01.00
1  of  4

How do I display the IP address by expanding the variable that is named after a variable...

I've tried all combinations of delayed expansion without luck. I want to recall this IP address for other functions also, not just display purposes.

Any direction would be very much appreciated!!

1 Answers

You already knew the trick - use CALL %%...%%

call echo. Machine: %%!Machine[%EventCount%]!%%    on   %%Machine%%

Btw. This block:

call set EventTime[%%EventCount%%]=%%a 
call set Machine[%%EventCount%%]=%%b
call set Decision[%%BagCount%%]=%%c

should be changed to

set EventTime[!EventCount!]=%%a 
set Machine[!EventCount!]=%%b
set Decision[!BagCount!]=%%c

Because, CALL is much slower.

The CALL command works, because it restarts the batch parser and one more percent expansion occurs, but it also has some unwanted side effects, like doubling/removing of carets and evaluation of special characters like &|<>.

Therefore a line like

call set EventTime[%%EventCount%%]=%%a 

would fail, if %%a contains Cat&Dog

Related