Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @ECHO OFF & SetLocal EnableDelayedExpansion
- REM Define values to be sorted, and create calls unique to those values.
- REM Demonstrates a technique by which varying numbers of variables can be appended to a call for sorting
- REM The same Technique can be used to build the sort_Vars String within a For Loop when processing unknown numbers of variables
- Set "sort_Vars=one,two,three,four,five,six,seven,eight,nine,ten"
- Set "@CALL_1=CALL :Sort_L_to_H Number_L i_1"
- Set "@CALL_2=CALL :Sort_H_to_L Number_H i_2"
- Set "@Display_Vars=Echo("
- For %%A in (%sort_Vars%) Do (
- Set /A "%%A=!random! * 1500 / 32768 + 1"
- Set "@CALL_1=!@CALL_1! %%A"
- Set "@CALL_2=!@CALL_2! %%A"
- Set "@Display_Vars=!@Display_Vars! %%A !%%A!"
- )
- ECHO(
- ECHO( Original Order:
- %@Display_Vars% & REM Displays Original order of variable Values
- ECHO(
- ECHO( Sorted Low to High:
- %@CALL_1% & REM Calls Low to High sort Subroutine
- REM Array display - Does not change values of original variables
- ::: For /L %%A In (1,1,!i_1!) Do (ECHO(!Number_L[%%A]!)
- REM Prepares Array Display - Changes the values of the Original variables according to sort results.
- Set Index=0
- Set "@Display_L=Echo("
- For %%A In (%sort_Vars%) Do (
- Set /A "Index+=1"
- For %%B IN (!Index!) Do (Set "%%A=!Number_L[%%B]!")
- Set "@Display_L=!@Display_L! %%A !%%A!"
- )
- %@Display_L% & REM Displays L-H sorted order of variable Values
- ECHO(
- REM Array display - Does not change values of original variables
- ::: For /L %%A In (1,1,!i_1!) Do (ECHO(!Number_L[%%A]!)
- REM Prepares Array Display - Changes the values of the Original variables according to sort results.
- ECHO( Sorted High to Low:
- %@CALL_2% & REM Calls High to Low sort Subroutine
- Set Index=0
- Set "@Display_H=Echo("
- For %%A In (%sort_Vars%) Do (
- Set /A "Index+=1"
- For %%B IN (!Index!) Do (Set "%%A=!Number_H[%%B]!")
- Set "@Display_H=!@Display_H! %%A !%%A!"
- )
- %@Display_H% & REM Displays H-L sorted order of variable Values
- ECHO(
- REM Array display - Does not change values of original variables
- ::: For /L %%A In (1,1,!i_2!) Do (ECHO(!Number_H[%%A]!)
- pause
- Exit
- REM the Above demonstrates the functionality of the subroutines. Only `@ECHO OFF & SetLocal EnableDelayedExpansion` is required.
- ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: Subroutines for Population of Arrays with numeric values in sorted order.
- :sort_L_to_H <Element_VarName> <Element_Index_VarName> <Variable Names containing the values to be Sorted and Populated to the Array>
- REM Adjust array Index Number to account for Element_VarName and Element_Index_VarName:
- Set "%2=-2"
- FOR %%P In (%*) DO (
- Set /A "%2+=1"
- Set "%1[!%2!]=!%%~P!"
- )
- REM Compensate for 0 in determining the range of the Array Index
- Set /A "%2+=1"
- REM Sort the Array using an Offset and Temporary variable to exchange values within nested For Loops.
- For /L %%a In (2,1,!%2!) Do (
- Set /A "S_Offset=%%a - 1"
- For /L %%b IN (1,1,%%a) DO (
- For %%c in (!S_Offset!) DO (
- IF !%1[%%c]! LSS !%1[%%b]! (
- Set "tmp=!%1[%%c]!"
- Set "%1[%%c]=!%1[%%b]!"
- Set "%1[%%b]=!tmp!"
- )
- )
- )
- )
- REM restore correct array range to Element_Index_Varname
- Set /A "%2-=1"
- Exit /B
- :sort_H_to_L <Element_VarName> <Element_Index_VarName> <Variable Names containing the values to be Sorted and Populated to the Array>
- REM Adjust array Index Number to account for Element_VarName and Element_Index_VarName:
- Set %2=-2
- FOR %%P In (%*) DO (
- Set /A "%2+=1"
- Set "%1[!%2!]=!%%~P!"
- )
- REM Compensate for 0 in determining the range of the Array Index
- Set /A "%2+=2"
- REM Sort the Array using an Offset and Temporary variable to exchange values within nested For Loops.
- For /L %%a In (2,1,!%2!) Do (
- Set /A "S_Offset=%%a - 1"
- For /L %%b IN (1,1,%%a) DO (
- For %%c in (!S_Offset!) DO (
- IF !%1[%%c]! GTR !%1[%%b]! (
- Set "tmp=!%1[%%c]!"
- Set "%1[%%c]=!%1[%%b]!"
- Set "%1[%%b]=!tmp!"
- )
- )
- )
- )
- REM restore correct array range to Element_Index_Varname
- Set /A "%2-=2"
- Exit /B
Advertisement
Add Comment
Please, Sign In to add comment