China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-08-09 20:03
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Experts, please help optimize the code View 2,836 Replies 41
Original Poster Posted 2008-09-12 04:51 ·  中国 江苏 苏州 电信
中级用户
★★
Credits 209
Posts 100
Joined 2008-08-01 02:53
18-year member
UID 122532
Gender Male
Status Offline
R02 u70.843 i-19.91 5-10.17 I5.641
U71.796 B-18.487 5-8.11 I4.556
G75.13 H-10.666 5-9.16 I-9.209
F75.13 5-7.27 H-10.666 I-9.209

Among the above, the number after 5- is 10.17 which is the largest, so display 5-10.17


@echo off&setlocal enabledelayedexpansion
for /f "delims=" %%a in (a.txt) do (
for %%i in (%%a) do (
set "var=%%i"
if "!var:~,1!" equ "5" (
set "var= !var!"
set "_!var:~-10!=%%i"
)
)
)
for /f "tokens=2 delims==" %%i in ('set _') do set "max=%%i"
echo %max%
pause

The code works. However, if it's executed on a 10MB text, the execution efficiency is extremely slow, taking almost half an hour. VBS is also okay, as long as it's faster.

[ Last edited by dragonamd on 2008-9-12 at 05:10 AM ]
Floor 2 Posted 2008-09-12 11:16 ·  中国 湖南 株洲 电信
金牌会员
★★★★
永远的学习者
Credits 3,105
Posts 1,276
Joined 2008-03-08 13:00
18-year member
UID 112398
Gender Male
Status Offline
The efficiency problem of the code lies in set _. The modification is as follows:

@echo off&setlocal enabledelayedexpansion
for /f "delims=" %%a in (a.txt) do (
for %%i in (%%a) do (
set "var=%%i"
if "!var:~,2!" equ "5-" (
if not defined max set "max=!var:~2!"
if !var:~2! geq !max! set "max=!var:~2!"
)
)
)
echo %max%&pause>nul
批处理之家新域名:www.bathome.net
Floor 3 Posted 2008-09-12 11:39 ·  中国 湖南 株洲 电信
金牌会员
★★★★
永远的学习者
Credits 3,105
Posts 1,276
Joined 2008-03-08 13:00
18-year member
UID 112398
Gender Male
Status Offline
```
@echo off&setlocal enabledelayedexpansion
for /f "delims=" %%a in (a.txt) do (
for %%b in (%%a) do (
set "var=%%b"
if "!var:~,2!" equ "5-" (
if not defined max set "max=!var:~2!"
for /f "tokens=1* delims=." %%c in ("!max!") do set "max1=%%c"&set "max2=%%d"
for /f "tokens=1* delims=." %%e in ("!var:~2!") do (
if %%e gtr !max1! (
set "max=!var:~2!"
) else (
if %%e equ !max1! if %%d gtr !max2! set "max=!var:~2!"
         )
)
)
)
)
echo %max%&pause>nul
```
批处理之家新域名:www.bathome.net
Floor 4 Posted 2008-09-12 12:54 ·  中国 江苏 苏州 中移铁通
银牌会员
★★★
Credits 1,608
Posts 780
Joined 2007-10-07 10:19
18-year member
UID 99089
Gender Male
Status Offline
Borrow the S11S1 code to write a VBS

set fso=createobject("scripting.filesystemobject")
set f=fso.opentextfile("a.txt",1)

while f.atendofstream <> true
r=r&split(split(f.readline,"5-")(1))(0)&" " 'Extract all numbers after 5-
wend
r=split(r) 'Convert to array
n=ubound(r) 'Determine the maximum subscript of the array
for i=0 to n-1 'Convert to numeric type
r(i)=cdbl(r(i))
next
quicksort r,0,n
msgbox r(n-1)

'Quick sort:
'Division:
function partition(r,l,h)
dim i,j,t
i=l
j=h
t=r(i)'Initialize, t is the reference
do
while r(j)>=t and i<j
j=j-1'Scan from right to left, find the first number less than t
wend
if i<j then
r(i)=r(j)'Swap r(i) and r(j)
i=i+1
end if
while r(i)<=t and i<j
i=i+1'Scan from left to right, find the first number greater than t
wend
if i<j then
r(j)=r(i)'Swap r(i) and r(j)
j=j-1
end if
loop while i<>j
r(i)=t'Reference t has been finally positioned
partition=i
end function
'Sort:
sub quicksort(r,s1,t1)
dim i
if s1<t1 then'No need to sort when there is one number or no number
i=partition(r,s1,t1)'Divide r(s1) to r(t1)
quicksort r,s1,i-1'Recursively process the left interval
quicksort r,i+1,t1'Recursively process the right interval
end if
end sub


[ Last edited by huahua0919 on 2008-9-12 at 01:06 PM ]
Floor 5 Posted 2008-09-12 15:27 ·  中国 江苏 苏州 电信
中级用户
★★
Credits 209
Posts 100
Joined 2008-08-01 02:53
18-year member
UID 122532
Gender Male
Status Offline
The code has all been tested. Still, it doesn't speed up. The 10MB text is still as slow.
Floor 6 Posted 2008-09-12 15:58 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
20-year member
UID 59080
Status Offline
Try with gawk...
Floor 7 Posted 2008-09-13 02:37 ·  中国 江苏 苏州 电信
中级用户
★★
Credits 209
Posts 100
Joined 2008-08-01 02:53
18-year member
UID 122532
Gender Male
Status Offline
Floor 8 Posted 2008-09-13 12:07 ·  中国 湖北 武汉 联通
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
20-year member
UID 59080
Status Offline
Originally posted by dragonamd at 2008-9-13 02:37:
Can't write,

You can learn by yourself if you can't write.
gawk "{for (i=1;i<=NF;i++) { if($i~/^5-/){ split($i,a,/-/); if(a>max) max=a; }}} END{print max}" your_file


[ Last edited by lxmxn on 2008-9-13 at 12:09 PM ]
Floor 9 Posted 2008-09-13 12:17 ·  中国 江苏 苏州 电信
中级用户
★★
Credits 209
Posts 100
Joined 2008-08-01 02:53
18-year member
UID 122532
Gender Male
Status Offline
It doesn't work after running. I really don't understand.
Floor 10 Posted 2008-09-13 12:26 ·  中国 湖北 武汉 联通
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
20-year member
UID 59080
Status Offline
gawk is a tool that needs to be downloaded. Just search online.

It runs normally here.

ps It is suggested to take a look at the top-sticked batch processing novice FAQ.
Floor 11 Posted 2008-09-14 00:07 ·  中国 江苏 苏州 电信
中级用户
★★
Credits 209
Posts 100
Joined 2008-08-01 02:53
18-year member
UID 122532
Gender Male
Status Offline
Floor 12 Posted 2008-09-14 02:18 ·  中国 重庆 九龙坡区 电信
版主
★★★★★
Credits 9,023
Posts 5,017
Joined 2007-05-31 19:39
19-year member
UID 89899
Gender Male
Status Offline
It can be batch processed, but according to your requirements, the efficiency of batch processing is not as high as that of gawk.
Floor 13 Posted 2008-09-14 19:48 ·  中国 江苏 苏州 电信
中级用户
★★
Credits 209
Posts 100
Joined 2008-08-01 02:53
18-year member
UID 122532
Gender Male
Status Offline
I don't know how to install and use that tool
Floor 14 Posted 2008-09-14 19:50 ·  中国 重庆 九龙坡区 电信
版主
★★★★★
Credits 9,023
Posts 5,017
Joined 2007-05-31 19:39
19-year member
UID 89899
Gender Male
Status Offline
No need to install, just download gawk.exe and you can call it directly in batch processing. If you don't know how to use it, you can view the help.
Floor 15 Posted 2008-09-14 21:23 ·  中国 江苏 苏州 电信
中级用户
★★
Credits 209
Posts 100
Joined 2008-08-01 02:53
18-year member
UID 122532
Gender Male
Status Offline
I've been searching online for a long time and can't download it. Can you provide a download?
Forum Jump: