T3RRYT3RR0R

Batch populate Array and sort Numeric values.

Feb 14th, 2020
467
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 4.01 KB | None | 0 0
  1. @ECHO OFF & SetLocal EnableDelayedExpansion
  2.  
  3. REM Define values to be sorted, and create calls unique to those values.
  4.  
  5. REM Demonstrates a technique by which varying numbers of variables can be appended to a call for sorting
  6. REM The same Technique can be used to build the sort_Vars String within a For Loop when processing unknown numbers of variables
  7.  
  8.     Set "sort_Vars=one,two,three,four,five,six,seven,eight,nine,ten"
  9.     Set "@CALL_1=CALL :Sort_L_to_H Number_L i_1"
  10.     Set "@CALL_2=CALL :Sort_H_to_L Number_H i_2"
  11.     Set "@Display_Vars=Echo("
  12.  
  13.     For %%A in (%sort_Vars%) Do (
  14.         Set /A "%%A=!random! * 1500 / 32768 + 1"
  15.         Set "@CALL_1=!@CALL_1! %%A"
  16.         Set "@CALL_2=!@CALL_2! %%A"
  17.         Set "@Display_Vars=!@Display_Vars! %%A !%%A!"
  18.     )
  19.  
  20. ECHO(
  21. ECHO( Original Order:
  22. %@Display_Vars% & REM Displays Original order of variable Values
  23. ECHO(
  24. ECHO( Sorted Low to High:
  25. %@CALL_1% & REM Calls Low to High sort Subroutine
  26.  
  27. REM Array display - Does not change values of original variables
  28. ::: For /L %%A In (1,1,!i_1!) Do (ECHO(!Number_L[%%A]!)
  29.  
  30. REM Prepares Array Display - Changes the values of the Original variables according to sort results.
  31.     Set Index=0
  32.     Set "@Display_L=Echo("
  33.     For %%A In (%sort_Vars%) Do (
  34.         Set /A "Index+=1"
  35.         For %%B IN (!Index!) Do (Set "%%A=!Number_L[%%B]!")
  36.         Set "@Display_L=!@Display_L! %%A !%%A!"
  37.     )
  38.        
  39. %@Display_L% & REM Displays L-H sorted order of variable Values
  40. ECHO(
  41.  
  42. REM Array display - Does not change values of original variables
  43. ::: For /L %%A In (1,1,!i_1!) Do (ECHO(!Number_L[%%A]!)
  44.  
  45. REM Prepares Array Display - Changes the values of the Original variables according to sort results.
  46. ECHO( Sorted High to Low:
  47. %@CALL_2% & REM Calls High to Low sort Subroutine
  48.  
  49.     Set Index=0
  50.     Set "@Display_H=Echo("
  51.     For %%A In (%sort_Vars%) Do (
  52.         Set /A "Index+=1"
  53.         For %%B IN (!Index!) Do (Set "%%A=!Number_H[%%B]!")
  54.         Set "@Display_H=!@Display_H! %%A !%%A!"
  55.     )
  56.  
  57. %@Display_H% & REM Displays H-L sorted order of variable Values
  58. ECHO(
  59.  
  60. REM Array display - Does not change values of original variables
  61. ::: For /L %%A In (1,1,!i_2!) Do (ECHO(!Number_H[%%A]!)
  62.  
  63. pause
  64. Exit
  65.  
  66. REM the Above demonstrates the functionality of the subroutines. Only `@ECHO OFF & SetLocal EnableDelayedExpansion` is required.
  67.  
  68. ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: Subroutines for Population of Arrays with numeric values in sorted order.
  69.  
  70. :sort_L_to_H <Element_VarName> <Element_Index_VarName> <Variable Names containing the values to be Sorted and Populated to the Array>
  71. REM Adjust array Index Number to account for Element_VarName and Element_Index_VarName:
  72.     Set "%2=-2"
  73.    
  74.     FOR %%P In (%*) DO (
  75.         Set /A "%2+=1"
  76.         Set "%1[!%2!]=!%%~P!"
  77.     )
  78. REM Compensate for 0 in determining the range of the Array Index
  79.     Set /A "%2+=1"
  80. REM Sort the Array using an Offset and Temporary variable to exchange values within nested For Loops.
  81.     For /L %%a In (2,1,!%2!) Do (
  82.         Set /A "S_Offset=%%a - 1"
  83.         For /L %%b IN (1,1,%%a) DO (
  84.             For %%c in (!S_Offset!) DO (
  85.                 IF !%1[%%c]! LSS !%1[%%b]! (
  86.                     Set "tmp=!%1[%%c]!"
  87.                     Set "%1[%%c]=!%1[%%b]!"
  88.                     Set "%1[%%b]=!tmp!"
  89.                 )
  90.             )
  91.         )
  92.     )
  93. REM restore correct array range to Element_Index_Varname
  94.     Set /A "%2-=1"
  95.     Exit /B
  96.  
  97. :sort_H_to_L <Element_VarName> <Element_Index_VarName> <Variable Names containing the values to be Sorted and Populated to the Array>
  98. REM Adjust array Index Number to account for Element_VarName and Element_Index_VarName:    
  99.     Set %2=-2
  100.    
  101.     FOR %%P In (%*) DO (
  102.         Set /A "%2+=1"
  103.         Set "%1[!%2!]=!%%~P!"
  104.     )
  105. REM Compensate for 0 in determining the range of the Array Index
  106.     Set /A "%2+=2"
  107.  
  108. REM Sort the Array using an Offset and Temporary variable to exchange values within nested For Loops.
  109.     For /L %%a In (2,1,!%2!) Do (
  110.         Set /A "S_Offset=%%a - 1"
  111.         For /L %%b IN (1,1,%%a) DO (
  112.             For %%c in (!S_Offset!) DO (
  113.                 IF !%1[%%c]! GTR !%1[%%b]! (
  114.                     Set "tmp=!%1[%%c]!"
  115.                     Set "%1[%%c]=!%1[%%b]!"
  116.                     Set "%1[%%b]=!tmp!"
  117.                 )
  118.             )
  119.         )
  120.     )
  121. REM restore correct array range to Element_Index_Varname
  122.     Set /A "%2-=2"
  123.     Exit /B
Advertisement
Add Comment
Please, Sign In to add comment