1

请问如何根据孔大小参数“排序”多维数组?

例如:一个简单的例子是(从文本文件加载):

> Liv1.HoleSize[0] = 22 Liv1.HoleX[0] = 250 Liv1.HoleY[0] = -55
> Liv1.HoleSize[1] = 14 Liv1.HoleX[1] = 750 Liv1.HoleY[1] = 0
> Liv1.HoleSize[2] = 22 Liv1.HoleX[2] = 900 Liv1.HoleY[2] = -55

然后必须导致:

> Liv1.HoleSize[0] = 14 Liv1.HoleX[0] = 750 Liv1.HoleY[0] = 0
> Liv1.HoleSize[1] = 22 Liv1.HoleX[1] = 250 Liv1.HoleY[1] = -55
> Liv1.HoleSize[2] = 22 Liv1.HoleX[2] = 900 Liv1.HoleY[2] = -55
4

1 回答 1

4

由于 VBScript 没有原生排序,因此您必须推出自己的排序,或者从朋友那里获得一点帮助。

如果您的任务是按指定顺序将输入文件(给定的逐字)排序到输出文件,那么sort.exe您的朋友是:

  Dim sIn : sIn = "..\data\in00.txt"
  WScript.Echo readAllFromFile(sIn)
  WScript.Echo "-----------"
  Dim sCmd : sCmd = "sort /+19 " & qq(resolvePath(sIn))
  Dim aRet : aRet = goWSLib.Run(sCmd)
  If aRet(0) Then
     ' handle error
  Else
     WScript.Echo aRet(2)
  End If

输出:

================================================================
Liv1.HoleSize[0] = 22 Liv1.HoleX[0] = 250 Liv1.HoleY[0] = -55
Liv1.HoleSize[1] = 14 Liv1.HoleX[1] = 750 Liv1.HoleY[1] = 0
Liv1.HoleSize[2] = 22 Liv1.HoleX[2] = 900 Liv1.HoleY[2] = -55

-----------
Liv1.HoleSize[1] = 14 Liv1.HoleX[1] = 750 Liv1.HoleY[1] = 0
Liv1.HoleSize[0] = 22 Liv1.HoleX[0] = 250 Liv1.HoleY[0] = -55
Liv1.HoleSize[2] = 22 Liv1.HoleX[2] = 900 Liv1.HoleY[2] = -55

================================================================

如果类似的东西解决了您的问题,请直接说出来,我们可以讨论库函数中的支持代码。

但是,如果您必须(要)将输入文件解析(d)为二维数组,那么您可以得到的最好的朋友是断开连接的 ADODB 记录集:

  Dim aData : aData = Split(Join(Array( _
          "22 250 -55" _
        , "14 750 0"   _
        , "22 900 -55" _
        , "11 222 333" _
  )))
  Dim afData(3, 2)
  Dim nRows : nRows = UBound(afData, 1)
  Dim nCols : nCols = UBound(afData, 2)
  Dim i, r, c
  For i = 0 TO UBound(aData)
      r = i \   nRows
      c = i Mod (nCols + 1)
      afData(r, c) = aData(i)
'      WScript.Echo i, r, c, aData(i)
  Next
  For r = 0 To nRows
      For c = 0 To nCols
          WScript.StdOut.Write vbTab & afData(r, c)
      Next
      WScript.Echo
  Next
  WScript.Echo "-----------------"
  Dim oRS : Set oRS = CreateObject("ADODB.Recordset")
  For c = 0 To nCols
      oRS.Fields.Append "Fld" & c, adInteger
  Next
  oRS.Open
  For r = 0 To nRows
      oRS.AddNew
      For c = 0 To nCols
          oRS.Fields(c).value = afData(r, c)
      Next
      oRS.UpDate
  Next
  oRS.Sort = "Fld0"
  WScript.Echo oRS.GetString(adClipString, , vbTab, vbCrLf)
  WScript.Echo "-----------------"
  oRS.Sort = "Fld2"
  WScript.Echo oRS.GetString(adClipString, , vbTab, vbCrLf)

输出:

========================================
        22      250     -55
        14      750     0
        22      900     -55
        11      222     333
-----------------
11      222     333
14      750     0
22      250     -55
22      900     -55

-----------------
22      250     -55
22      900     -55
14      750     0
11      222     333

========================================

再说一遍:如果这看起来很有希望,我们可以讨论如何根据您的需要调整/简化此概念验证代码。

于 2012-09-05T20:45:42.943 回答