Advertisement
T3RRYT3RR0R

Array Shift / Comparison

Jan 1st, 2020
494
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 2.25 KB | None | 0 0
  1. @REM this script is only to demonstrate the mechanics of Comparing Arrays and Shifting Arrays.
  2.  
  3. @ECHO OFF
  4. SETLOCAL ENABLEDELAYEDEXPANSION
  5. Set Max=5
  6. Set /p "Max=[Enter Array Range:]
  7. REM - Account for Element 0:
  8. Set /a Max-=1
  9.  
  10. :main
  11. Set "Val=0"
  12. cls
  13.  
  14. REM - Use for /L loop to assign values to array elements using Max as the total number of elements.
  15.  
  16. For /L %%a in (0,1,!Max!) DO (
  17.                 Set /p "Array[!Val!]=[Enter Array[!Val!] Value:]"
  18.             If not defined Array[!Val!] (
  19.             goto main
  20.     )
  21.     Set /a Val+=1
  22. )
  23.  
  24. REM - Call function to display values assigned to array elements
  25. CALL :Display
  26. pause
  27.  
  28. REM - Tacks ann Element onto the end of the Array
  29. REM - User inputs the New Elements Value.
  30. REM - For Loop creats an Array for comparison
  31. REM - The example Clones the Original Array and shifts the the Elements using a tmp offset Variable - !Val_O!
  32. REM - Array value Set has to be Called as a Function, It will not Set the Value withing the Loop.
  33.  
  34. :append
  35. Set /a Max+=1
  36. cls
  37. Set cvo=0
  38. For /L %%a in (0,1,!Max!) DO (
  39.         Set cva=%%a
  40.         Call :Increment
  41.         IF NOT DEFINED Array[!cvo!] (
  42.         Set /p comp[%%a]=[Enter comp[!cvo!] Value:]
  43. )
  44.         IF !comp[%%a]!==!comp[%Max%]! (
  45.         Set /p Array[!Max!]=[Enter Array[!Max!] Value:]
  46.         Goto Comparison
  47.     )
  48. )
  49.  
  50. REM - For /L loop displays the values Assigned to each Element in the Comparison Array
  51. :Comparison
  52. CALL :Display
  53. For /L %%a in (0,1,!Max!) DO (
  54.             ECHO comp[%%a] = !comp[%%a]!
  55. )
  56.  
  57. pause
  58. cls
  59.  
  60. REM - For /L Loop Compares the Array and Substitutes Values If Arrays don't Match.
  61. REM - Action taken is for Demonstration Purposes.
  62. REM - the Action Demonstrated executes shifting the values of the Arrays Elements using the Clone/Offset Array.
  63.  
  64. :process
  65. For /L %%a in (0,1,!Max!) DO (
  66.             IF NOT !Array[%%a]!==!Comp[%%a]! (
  67.         ECHO Array[%%a] !Array[%%a]! NEQ !Comp[%%a]! Comp[%%a]
  68.         ECHO Values Exchanged for Array[%%a]
  69.         Set "Array[%%a]=!comp[%%a]!"
  70.     ) else (
  71.     ECHO Values Matched for Array[%%a]
  72.     )
  73. )
  74. pause
  75.  
  76. CALL :Display
  77.  
  78. :end
  79. Pause
  80. EXIT
  81.  
  82. :Increment
  83. Set /a cvo+=1
  84. Set comp[%cva%]=!Array[%cvo%]!
  85. GOTO :EOF
  86.  
  87. GOTO :EOF
  88.  
  89. REM - Function that uses For /L loop to display the values of all currently assigned Elements.
  90. :Display
  91. cls
  92. For /L %%a in (0,1,!Max!) DO (
  93.             ECHO array[%%a] = !array[%%a]!
  94. )
  95. GOTO :EOF
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement