Advertisement
T3RRYT3RR0R

Batch Populate multiple arrays - Function

Jan 4th, 2020
355
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 1.50 KB | None | 0 0
  1. @ECHO OFF
  2.  
  3. REM :: Rem out @ECHO OFF to see the demo step by step.
  4.  
  5. MODE 1000
  6. setlocal enabledelayedexpansion
  7.  
  8. REM :: To define string Values, enclose the string in Doubleqoutes: "text here" and Expand using %%~a within the For loop to
  9. REM :: strip the Doublequotes. Same is recommended for population of arrays with Filepaths.
  10.  
  11. CALL :define example e_M 2 4 6 8 10 12 14 16 18 20
  12. CALL :display example !e_M!
  13. CALL :define demo d_M 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29
  14. CALL :display demo !d_M!
  15. pause
  16. exit
  17.  
  18.  
  19. :define
  20. REM - Assigns parameters 3+ to array index 0 onwards
  21. REM - Call :define <arrayname> <MaxValueVar> <index0Val> ~ <indexLastVal>
  22.  
  23. REM - Strip Array name and MaxValue Variable name from processing
  24.  
  25. Set count=-2
  26.  
  27. FOR %%a in (%*) DO (
  28.     Set "%1[!count!]=%%a"
  29.     Set /a count+=1
  30. )
  31.  
  32. REM - Establishes Array Range from Index 0-Last, Accounting for Index 0
  33.  
  34. Set /a %2=!count!-1
  35. GOTO :EOF
  36.  
  37. :display
  38.  
  39. REM - Loops through Index 0-Last.
  40. REM - Call :display <arrayname> <MaxValueVar>
  41.  
  42. REM - This type of For loop can be used to Display or test Index values - Or - perform actions using Array values.
  43.  
  44. For /L %%a IN (0,1,%2) DO (
  45. ECHO %1[%%a] = !%1[%%a]!
  46. )
  47.  
  48.  
  49. REM - Account for Index 0 in Array range.
  50.  
  51. Set /a ttl=1 + %2
  52. ECHO.
  53. ECHO  %1 Contains [%ttl%] Elements
  54. ECHO.
  55. GOTO :EOF
  56.  
  57. REM :: To access an individual array elements value, expand it as follows: !%ArrayName%[IndexNumber]!
  58. REM :: To modify an individual array elements value, Set it as follows: Set "ArrayName[IndexNumber]=Value"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement