Page 1 of 1

Dynamically load array with variable value

Posted: 27 Jul 2020 20:18
by Surya
I have a param variable which is dynamic ie.it can have N number of values delimited by spaces.As an example the below param has two values.But it can 3 or 4 values delimted by space.each key value pair will be separated by &.This is basically the syntax of tabcmd get.

SET PARAM=productname=%%a&productid=%%b empid=%%a&empname=%%b&empad=%%c ...
My requirement is to parse param not as string and load each of the values that are delimited by space into an array.Please note that array indexing should be dynamic since the number of parameters in param is dynamic.So in the above case there will be two array index, a[0]=productname=%%a&productid=%%b a[1]=empid=%%a&empname=%%b&empad=%%c

There can be a[3] or a[4] based on the number of parameter values. So the index should be dynamic.

Once the array is ready, I will feeding this inside a for loop which reads a file having the actual values for the variables mentioned in the param(product name,productid in the first iteration and emp details in the second iteration since we have only two values in this case).So there %%a, %%b, %%c should get replaced with the read actual values.

Why I mentioned not as a string is that if we parse param as a string then the %%a, %%b values wont get replaced with actual values from file. I tired to read param in for loop without adding quotes then an error is throwing & was not expecting at this time

Can someone provide suggestions to load the array dynamically without considering the param as string and the array index should be dynamic.

TIA

Re: Dynamically load array with variable value

Posted: 28 Jul 2020 09:04
by Aacini
Your question is confusing. There are several points that are not clear enough... However, I think this code does what you requested:

Code: Select all

@echo off
setlocal EnableDelayedExpansion

SET "PARAM=productname=%%a&productid=%%b empid=%%a&empname=%%b&empad=%%c"

rem Parse "PARAM" and load space-delimited values into a (one-based) array
set N=0
for %%a in ("%PARAM: =" "%") do (
   set /A N+=1
   set "line=%%~a"
   set "a[!N!]=!line:%%=%%%%!"
)

rem Show all values in array
set a[

rem Feed the values in the array via File.txt data file
rem First iteration for first variable, second iteration for second variable, etc.
set "i=0"
for /F "tokens=1,2,3,4" %%a in (File.txt) do (      
   set /A i+=1
   for %%i in (!i!) do (
      call :FeedValues "!a[%%i]:&=& set !"
      rem Show feeded values
      for %%x in ("!a[%%i]:&=" "!") do for /F "delims==" %%y in (%%x) do echo %%y=!%%y!
   )
)
goto :EOF


:FeedValues 
for %%? in (_) do set %~1
exit /B
This is File.txt:

Code: Select all

ThisIsProductName AndProductId
EmpId EmpName EmpAd
... and this is the output:

Code: Select all

a[1]=productname=%%a&productid=%%b
a[2]=empid=%%a&empname=%%b&empad=%%c
productname=ThisIsProductName
productid=AndProductId
empid=EmpId
empname=EmpName
empad=EmpAd
The only difference vs. your example is that the array is one-based instead of zero-based, that is, the first element is index 1, not 0.

Antonio

Re: Dynamically load array with variable value

Posted: 28 Jul 2020 10:47
by Surya
Thank u so much for the answer.It closely matches.However let me clarify the confusion.Its been a week am struggling with this.PFB my script
for /f "tokens=1-26 delims=," %%a in (File.txt) do (
tabcmd.exe get "%param%"
I want the output to be like
tabcmd.exe get "?productname=ThisIsProductName&productid=AndProductId"

I will iterate the above part to get the emp details.

Re: Dynamically load array with variable value

Posted: 28 Jul 2020 15:58
by Aacini
In your first post you clearly specified to split the PARAM string in array elements, but later you don't even use such array elements! :shock:

In your first post it is assumed to extract the values of the variables (productname, productid), but no, you don't use such variables... :?

Ok. Here it is my second version:

Code: Select all

@echo off
setlocal EnableDelayedExpansion

SET "PARAMS=productname=%%a&productid=%%b empid=%%a&empname=%%b&empad=%%c"

for /F "tokens=1-26 delims=," %%a in (File.txt) do (
   for /F "tokens=1*" %%x in ("!PARAMS!") do set "param=%%x" & set "PARAMS=%%y"
   call :expandParam 
)
goto :EOF

:expandParam
for %%? in (_) do ECHO tabcmd.exe get "?%param%"
File.txt:

Code: Select all

ThisIsProductName,AndProductId
EmpId,EmpName,EmpAd
Output:

Code: Select all

tabcmd.exe get "?productname=ThisIsProductName&productid=AndProductId"
tabcmd.exe get "?empid=EmpId&empname=EmpName&empad=EmpAd"
Antonio