![]() |
China DOS Union-- Unite DOS · Advance DOS · Grow DOS --Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum |
| Guest | Log in | Register | Members | Search | China DOS Union |
|
中国DOS联盟论坛 The time now is 2026-09-15 12:24 |
47,812 topics / 349,917 posts / today 0 new / 48,268 members |
| DOS批处理 & 脚本技术(批处理室) » [Discussion] Constructing an array in batch processing to achieve ordered arrangement of numbers |
| Printable Version 3,750 / 7 |
| Floor1 pengfei | Posted 2006-11-15 00:16 |
| 银牌会员 Posts 485 Credits 1,218 From 湖南.娄底 | |
|
Suddenly I want to use batch processing to sort numbers. Previously, we compared several numbers respectively and arranged them in order, but when the number of numbers is small, if the number exceeds 10, the code will be very lengthy and the efficiency is relatively low.
If batch processing had the concept of array types like high-level languages, it would be great, but unfortunately. In batch processing, manually constructing an array can solve this problem. So I wrote the following code, which can sort N numbers in descending or ascending order. Just change "lss" or "gtr" in the IF statement. Test code: [ Last edited by pengfei on 2006-11-15 at 06:54 AM ] |
|
| Floor2 redtek | Posted 2006-11-15 00:43 |
| 金牌会员 Posts 1,147 Credits 2,902 | |
| Floor3 vkill | Posted 2006-11-15 01:28 |
| 金牌会员 Posts 1,744 Credits 4,103 From 甘肃.临泽 | |
|
You can first determine the number of bits, which should be more efficient.
|
|
| Floor4 pengfei | Posted 2006-11-15 01:32 |
| 银牌会员 Posts 485 Credits 1,218 From 湖南.娄底 | |
|
RE: vkill
The code not only has the function of judging the number of bits, but also has precise control over the number of times the loop runs, and the execution efficiency is relatively ideal. [ Last edited by pengfei on 2006-11-20 at 08:48 AM ] |
|
| Floor5 ccwan | Posted 2006-11-15 01:39 |
| 金牌会员 Posts 1,160 Credits 2,725 From 河北廊坊 | |
|
Nice
|
|
| Floor6 youxi01 | Posted 2006-11-15 08:11 |
| 高级用户 Posts 247 Credits 846 From 湖南==》广东 | |
|
Let's take a look at the test code of our "genius" moderator (it seems like 37.....moderator):
@echo off setlocal enabledelayedexpansion set numbers=%random% %random% %random% %random% %random% %random% %random% %random% echo The array before sorting is: %numbers% call :sort %numbers% echo The array after sorting is: %ret% pause goto :eof :sort set num=%1 set str=%* for %%i in (%str%) do if !num! gtr %%i set num=%%i set ret=%ret% %num% if not "!str:%num%=!" == "" call :sort !str:%num%=! goto :eof Is it more efficient! For decimal/ The following is the "applied" code improved from the above code: @echo off color 1f if "%1"=="" goto :instruction setlocal enabledelayedexpansion for %%i in (%1 %2 %3 %4 %5 %6 %7 %8 %9) do ( if not "%%i"=="" set number=!number! %%i) call :sort %number% echo. echo The above numbers are in descending order as follows: echo. echo %str% pause>nul :instruction cls echo. echo Number Sorting echo. echo ============================================================ echo Instructions: echo This program can only be said to basically meet the requirements; no filtering of numbers; echo Currently only supports sorting of up to 9 numbers! echo. echo Usage is as follows: [%~nx0] [%%1] [%%2] [%%3] [%%4] ........ echo ============================================================= echo. cmd /k :sort set var=%1 set var_=%* for %%i in (%var_%) do ( if !var! leq %%i set var=%%i) set str=!str! !var! set var_=!var_:%var%=! if not "%var_%"=="" call :sort %var_% |
|
| Floor7 3742668 | Posted 2006-11-15 08:40 |
| 荣誉版主 Posts 718 Credits 2,013 | |
|
Re youxi01:
To solve the limitation of 9 parameters, you can try using shift. Of course, a better method is to replace the for statement under setlocal enabledelayedexpansion in your code with: set "number=%*" |
|
| Floor8 pengfei | Posted 2006-11-15 09:40 |
| 银牌会员 Posts 485 Credits 1,218 From 湖南.娄底 | |
|
After looking at the code of moderator 3742668, I am really impressed...
Obviously, this method of constructing an array by arranging numbers with spaces in between is simpler. Then, read each array element one by one through the for /f command for comparison. Not only is it faster, but the code is also concise. I have learned something... |
|
|
[ Contact the Union admin team -
中国DOS联盟 -
Standard version ] Sponsored by ifanr Inc | © 2001–2023 |