2

How can I "Sort" the multidimensional arrays based on the hole size parameter please?

eg: A simple example would be (Loaded from Text file):

> 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

must then result in :

> 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
3
  • That looks like 3 independent arrays not a multidimensional array, its also not vbscript (it could be valid js) what vbscript data structure is Liv1 & its members? Commented Sep 5, 2012 at 13:27
  • Apologies, I am getting the script again and posting more info, thank you Commented Sep 5, 2012 at 14:31
  • Hi, you are correct There are 3 independent arrays Hole size,HoleX And HoleY, The data types are all "real" numbers When read from text file it comes in as string and I convert into real numbers. I am battling to find a method to sort these arrays , unfortunately I only have VBScript to work with Commented Sep 5, 2012 at 20:07

1 Answer 1

5

As VBScript has no native sort, you'll have to roll your own sort, or to get a little help from friends.

If your task is to sort your input file (verbatim as given) to an output file in the specified order, sort.exe is your friend:

  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

output:

================================================================
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

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

If something like that solves your problem, just say so, and we can talk the support code in the library functions.

If, however, you have (to) parse(d) the input file into a two-dimensional array, the best friend you can get is a disconnectes ADODB recordset:

  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)

output:

========================================
        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

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

Again: if that looks promising, we can discuss how to adapt/streamline this proof of concept code to your needs.

Sign up to request clarification or add additional context in comments.

4 Comments

I like disconnected recordsets a lot. So versatile.
hi, thank you so much, this looks amazing, I will sit tonight with a test file and reply , thank you!
hi, this is brilliant thanks, for the first time there are signs of hope, the last example you showed looks great. Can I ask please, I copied the code of the last block into c:\MyFile.vbs, and then in cmd prompt ran cscript MyFile.vbs, and got the error "C:\MyFile.vbs(26,7) ADODB.Fields: Arguments are of the wrong type, are out of acceptable range, or are in conflict with one another". I s there perhaps something wrong in the way I am executing it please? I tried to debug my side by replacing the "adInteger" with the digit "1" but still get the same error, thank you!
@PeterPitLock adInteger=3: adClipString=2 Have a look for adovbs.inc

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.