簡體   English   中英

VBScript對文件中的行進行排序

[英]VBScript to sort lines in a file

我正在嘗試使用VBScript(MS Internet Explorer)對文件中的行進行排序。 基本上,我讀取文件並將所有行轉換為數組。 基本上,我需要對每行字符23和27之間寫的內容進行排序。 所以基本上我可以使用MID獲得那些字符。 這些字符不是每行唯一的(因此我不能直接使用System.Collections.Sortedlist或類似的東西)。

我也許可以使用System.Collections.Sortedlist並使用23到27之間的字符作為鍵,並使用另一個System.Collections.Sortedlist對象作為一個值,並填充每個唯一值(即二維數組),但這是正確的方法嗎? 例如:

(System.Collections.Sortedlist) {
    "0001" => (System.Collections.Sortedlist) {
        0 => "0001 some unique value1"
        1 => "0001 some unique value2"
        2 => "0001 some unique value3"
    },
    "0002" => (System.Collections.Sortedlist) {
        0 => "0002 some unique value1"
        1 => "0002 some unique value2"
        2 => "0002 some unique value3"
    }
}

有沒有更好的方法來解決這個問題? 另請注意,輸出格式應再次為數組。

作為預告片(另請參見ArrayListDisconnected Recordset和ArrayList ):

  Dim aTest : aTest = Array( _
      ". Z 0" _
    , ", B 2" _
    , "? A 1" _
  )
  WScript.Echo "----- ArrayList"
  WScript.Echo "original data:     ", "[""" & Join(aTest                          , """  """) & """]"
  WScript.Echo "AL:  (full)        ", "[""" & Join(sortAL(aTest)                  , """  """) & """]"
  WScript.Echo "AL:  (numerical)   ", "[""" & Join(sortAL(Array(12, 9, -1))       , """  """) & """]"
  WScript.Echo "AL:  (dates)       ", "[""" & Join(sortAL(Array(Now + 5, Now - 5)), """  """) & """]"
  WScript.Echo "AL:  (Mid(elm,3,1))", "[""" & Join(sortAL_31(aTest)               , """  """) & """]"
  WScript.Echo "----- Disconnected Recordset"
  WScript.Echo "DRS: (Mid(elm,3,1))", "[""" & Join(sortDRS_Substring(aTest, 3, 1) , """  """) & """]"
  WScript.Echo "DRS: (Mid(elm,5,1))", "[""" & Join(sortDRS_Substring(aTest, 5, 1) , """  """) & """]"

' ArrayList.Sort handles homogenous arrays of simple types just fine,
' but looks at the element in full. For other cases, you need extra work
' or ADO
Function sortAL(aX)
  Dim al : Set al = CreateObject("System.Collections.ArrayList")
  Dim elm
  For Each elm In aX
      al.Add elm
  Next
  al.Sort
  sortAL = al.ToArray()
End Function

' Extra work: feeding pre-processed data to the ArrayList; hardcoded
Function sortAL_31(aX)
  Const csSep = ":"
  Dim al : Set al = CreateObject("System.Collections.ArrayList")
  ReDim a(UBound(aX))
  Dim elm
  For Each elm In aX
      al.Add Mid(elm, 3, 1) & csSep & elm
  Next
  al.Sort
  For elm = 0 To UBound(a)
      a(elm) = Split(al(elm), csSep)(1)
  Next
  sortAL_31 = a
End Function

' more flexible: ADO DRS + a start at parametrization; handling different
' types is left as an exercise
Function sortDRS_Substring(aX, nFrom, nLen)
  Const adVarChar    = 200 ' 000000C8
  Const adClipString =   2 ' 00000002
  Const csSep        = ":"
  ReDim a(Ubound(aX))
  Dim oRS : Set oRS = CreateObject("ADODB.Recordset")
  oRS.Fields.Append "FORG", adVarChar, 250
  oRS.Fields.Append "FSUB", adVarChar, 250
  oRS.Open
  Dim elm
  For Each elm In aX
      oRS.AddNew
      oRS.Fields("FORG").value = elm
      oRS.Fields("FSUB").value = Mid(elm, nFrom, nLen)
      oRS.UpDate
  Next
  oRS.Sort = "FSUB"
  oRS.MoveFirst
  For elm = 0 To UBound(a)
      a(elm) = oRS.Fields("FORG").value
      oRS.MoveNext
  Next
  sortDRS_Substring = a
End Function

輸出:

----- ArrayList
original data:      [". Z 0"  ", B 2"  "? A 1"]
AL:  (full)         [", B 2"  ". Z 0"  "? A 1"]
AL:  (numerical)    ["-1"  "9"  "12"]
AL:  (dates)        ["1/23/2013 5:00:22 PM"  "2/2/2013 5:00:22 PM"]
AL:  (Mid(elm,3,1)) ["? A 1"  ", B 2"  ". Z 0"]
----- Disconnected Recordset
DRS: (Mid(elm,3,1)) ["? A 1"  ", B 2"  ". Z 0"]
DRS: (Mid(elm,5,1)) [". Z 0"  "? A 1"  ", B 2"]

我想建議的功能幾乎與Ekkehard Horner的sortAL_31相同。

Function CustomSort(ByVal inpArray, ByVal nFrom, ByVal nLen)
    Dim oList, aTmp(), nSize, i
    nSize = UBound(inpArray)
    ReDim aTmp(nSize)
    With CreateObject("System.Collections.ArrayList")
        For i = 0 To nSize
            .Add Mid(inpArray(i), nFrom, nLen) & _
                vbNullChar & inpArray(i)
        Next
        .Sort
        For i = 0 To nSize
            aTmp(i) = Split(.Item(i), vbNullChar)(1)
        Next
    End With
    CustomSort = aTmp
End Function

[Edit]用SortedList附加版本。

Function CustomSort2(inpArray, nFrom, nLen)
    Dim oSList, aTmp(), nSize, i
    nSize = UBound(inpArray)
    ReDim aTmp(nSize)
    Set oSList = CreateObject("System.Collections.SortedList")
    For i = 0 To nSize
        oSList.Add Mid(inpArray(i), nFrom, nLen), inpArray(i)
    Next
    For i = 0 To nSize
        aTmp(i) = oSList.GetByIndex(i)
    Next
    Set oSList = Nothing
    CustomSort2 = aTmp
End Function

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM