2010-02-04 114 views
0
subject score studentid 
xx 23.22% 1 
yy 34% 2 
zz 55.2% 3 
xx 88.66% 4 
yy 23.76% 5 
zz 78.04% 6 

如何獲得每個主題的最大百分比和學生ID?從csv獲得最大百分比

+2

@stack - 你開始冒險進行實際的數據處理。我強烈推薦一些其他工具,而不是cmd腳本。批處理文件在這種類型的處理中表現不佳的地方太多了。使用vbscript,perl,basic,python - 幾乎除了Windows/DOS/cmd腳本之外的任何東西。看到這個答案爲指向一本好書對批處理文件以及更多的細節,爲什麼我建議你使用別的東西:http://stackoverflow.com/questions/180754/best-free-resource-for-learning-高級批處理文件使用情況/ 180767#180767 – 2010-02-04 07:32:19

+0

沒有perl但批處理。由於您的腳本在比較時遇到了一些問題......我們如何進行數字比較? – stack 2010-02-04 08:06:28

回答

0

這裏是一個VBScript你可以嘗試

Set objFS = CreateObject("Scripting.FileSystemObject") 
Set objArgs = WScript.Arguments 
Set d = CreateObject("Scripting.Dictionary") 
Set e = CreateObject("Scripting.Dictionary") 
strFile = objArgs(0) 
Set objFile = objFS.OpenTextFile(strFile) 
Do Until objFile.AtEndOfStream 
    strLine=objFile.ReadLine 
    s = Split(strLine," ") 
    subject =s(0) 
    score= Left(s(1),Len(s(1))-1) 
    studentid=s(2) 
    If Not d.Exists(subject) Then 
     d.Add subject, score 
     e.Add subject, studentid 
    Else 
     If score >= d.Item(subject) Then   
      d.Item(subject) = score 
      e.Item(subject) = studentid 
     End If 
    End If 
Loop 
j=d.Keys 
For Each stritems In j 
    WScript.Echo "Subject:"&stritems & ", Score: "& d.Item(stritems) & "%, StudentID: " & e.Item(stritems) 
Next 

輸出

C:\test>type file 
xx 23.22% 1 
yy 34% 2 
zz 55.2% 3 
xx 88.66% 4 
yy 23.76% 5 
zz 78.04% 6 

C:\test>cscript //nologo test.vbs file 
Subject:xx, Score: 88.66%, StudentID: 4 
Subject:yy, Score: 34%, StudentID: 2 
Subject:zz, Score: 78.04%, StudentID: 6 
0

你需要有某種形式的收集處理爲每個主題的總數,這可能幫助

@echo off 

    set CountXX=1 
    set CountYY=2 


    set This=XX 
    call :Resolve Count%This% 
    echo %RetVal% 


    set This=YY 
    call :Resolve Count%This% 
    echo %RetVal% 

    set /a Count%This%=%RetVal% + 2 

    call :Resolve Count%This% 
    echo %RetVal% 

goto :eof 


:Resolve 


    for /f "delims== tokens=2" %%a in ('set %1') do set retval=%%a 

goto :eof