Actually I am writing a file converter tool. The data values represent a 120×120 matrix. My first thought was two nested for loops and just a new line inside the first: Easy.
The input file has a vbNewLine each fith value.
I parsed the input file into a string strAll.
Input
strAll contains 10 character long values
" -24.1189" (two blanks before value)
" 1.2345" (four blanks before value)
Output
-24.1189;-24.1189; (...total 120 values...)
-24.1234;-24.1189; (...total 120 values...)
(... total 120 rows ...)
Using Mid hat should be easy to parse: Mid(strAll, 1+i, 10+i), where i is the counter in a for loop. A Replace(stAll, " ", "") should remove all the 3 blanks, 2 blanks and one blank.
Question: How to output the string to a file, formatted like a matrix?
Dim intValueLength, maxValue, intValueInRowMax
intValueLength=10
intValueInRowMax=120
maxValues=intValueInRowMax * intValueInRowMax
Sub Strg2Array
arrAll = array()
' convert to array
For i=1 To maxValues
ReDim Preserve arrAll(UBound(arrAll) +1)
arrAll(UBound(arrAll)) = Mid(strAll, 1+(i-1)*intValueLength, i*intValueLength)
Next
End Sub
Sub SaveAll
Dim intValueInRow
intValueInRow=0
Const ForWriting = 2
Set objFSOOut = CreateObject("Scripting.FileSystemObject")
Set objOutput = objFSOOut.OpenTextFile(strFileName, ForWriting, true)
for each value in arrAll
objOutput.Write value
intValueInRow = intValueInRow + 1 'Argh, there is no "++" operator?
If (intValueInRow = intValueInRowMax) Then
objOutput.Write vbNewLine
intValueInRow=0
End If
next
objOutput.Close
Set objFSOOutput = Nothing
End Sub
(Nearly) the same idea as Ansgar, different implementation:
output:
My pattern tries to identify the valid data and avoids the (maybe costly) string manipulations.