Advertisement
T3RRYT3RR0R

Batch Array Population and Nested Arrays

Feb 15th, 2020
355
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 1.46 KB | None | 0 0
  1.     @Echo Off & Setlocal EnableDelayedExpansion & Title Batch Nested Array & Goto :Main
  2.  
  3. :Populate <Element Var Name> <Element Index VarName>
  4.  
  5.     Set %2=0
  6.     For %%A in (!%1!) Do (
  7.         Set /A "%2+=1"
  8.         Set "%1[!%2!]=%%A"
  9.     )
  10.     Exit /B
  11.  
  12. :Main
  13.  
  14. REM Define values of variables within array indices (As Examples)
  15.     For %%A in (one,two,three,four,five) Do (
  16.         Set /A %%A=!random! * 1500 / 32768 + 1
  17. REM Build Array set list for Populate Subroutine. This method of building the set string can be applied to any For Loop.
  18.         If Defined group1 (Set "group1=!group1!,%%A") Else (Set "group1=%%A")
  19.     )
  20.  
  21.     For %%A in (_One,_Two,_Three,_Four,_Five) Do (
  22.         Set /A %%A=!random! * 1500 / 32768 + 1
  23.         If Defined group2 (Set "group2=!group2!,%%A") Else (Set "group2=%%A")
  24.     )
  25.  
  26. Call :Populate group1 _A
  27. Call :Populate group2 _B
  28.  
  29.     For %%A in (group1,group2) Do (If Defined Nest (Set "Nest=!Nest!,%%A") Else (Set "Nest=%%A"))
  30.     Call :Populate Nest _C
  31.  
  32. Rem Display Array Element Names and Values
  33.  
  34.     For /L %%A in (1,1,!_A!) Do (For %%B In (!group1[%%A]!) Do (ECHO(group1[%%A] : %%B = !%%B!))
  35.     For /L %%A in (1,1,!_B!) Do (For %%B In (!group2[%%A]!) Do (ECHO(group2[%%A] : %%B = !%%B!))
  36.  
  37. REM Accessing Variables from the Nested Array (Every Tier):
  38.  
  39.     FOR /L %%A in (1,1,!_C!) Do (
  40.         For %%B In (!Nest[%%A]!) Do (
  41.             ECHO(Nest[%%A] : !Nest[%%A]! = !%%B!
  42.             Set count=0
  43.             For %%C In (!%%B!) Do (
  44.                 Set /A count+=1
  45.                 ECHO(Nest[%%A] : %%B[!count!] = %%C = !%%C!)
  46.             )
  47.         )
  48.     )
  49.  
  50.     Pause
  51.     Exit
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement