Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do you write Do...Until to count block of lines?? 2

Status
Not open for further replies.

londonkiwi

Programmer
Sep 25, 2000
83
NZ
All I want to do is count how many line there are between the block (defined by the lines 21 to 23) and write that value into a new line eg PLINE (500). Can someone please show me how to write the Do..Until Loop correctly??
Thanks for your help!!

This is the MIF file

21 1156 1157 2629974 6611014
22 1156 1157 2629979 6611013
22 1156 1157 2629980 6611013
23 1156 1157 2629993 6610897
21 1158 1159 2630065 6611130
23 1158 1159 2630080 6611298

I want to see this

Pline (4)
.......lines of other data
Pline (2) etc

Private Sub Command5_Click()

Dim lCounter As Long
Dim lLine As Long
Dim sLine As String
Dim sNewLine As String
Dim FileNumRead As Integer
Dim FileNumWrite As Integer
Dim lData1 As Long

Dim I As Integer

Dim sFileName As String
sFileName = App.Path & "\Ex_Track_Crd.crd"
Dim sNewFile As String
sNewFile = App.Path & "\Crd_Mif.mif"
FileNumRead = FreeFile

Open sFileName For Input As #FileNumRead
FileNumWrite = FreeFile
Open sNewFile For Output As #FileNumWrite

lCounter = 0
lLine = 0

Line Input #FileNumRead, sLine ' get first line of data

Do Until (Left(sLine, 2)) = "21" Or EOF(FileNumRead)
Line Input #FileNumRead, sLine
Loop

Line Input #FileNumRead, sLine

Do Until EOF(FileNumRead)
' code here to process line of data, setting sData1-sData5
I = 1
Do Until Mid(sLine, I, 1) = " " 'find first marking character
lData1 = lData1 & Mid(sLine, I, 1)
I = I + 1
Loop

Do Until lData1 = 23
lCounter = lCounter + 1 ' count the number of lines from "21" to "23"
Loop

Print #FileNumWrite, "Pline" & "(" & lCounter & ")"
' write the number of lines
lCounter = 0

Line Input #FileNumRead, sLine ' get next line to see what number to begin output with

lData1 = 0
Loop

Close #FileNumRead
Close #FileNumWrite

MsgBox "Completed Reading Tracks CRD (Ex_Track_crd) and Writing PLINE MIF (crd_mif)"

End Sub [sig][/sig]
 
You can incorporate this into the main loop that is already there.
You do not need this loop:
Do Until lData1 = 23
lCounter = lCounter + 1 ' count the number of lines from "21" to "23"
Loop

All you need is a counter variable and then increment the counter after you have written a line to the file, and then when you retrieve the next line (the second Line Input line), check to see if you have passed a line beginning 23 (ie you should be on a line that begins '21.....') and then you can write out the PLine(X) line to the file.
However, this will get you a retrospective count of data lines. I.e, the output file will read:
line 1
line 2
line 3
line 4
PLine(4)
line 1
line 2
PLine(2)
......
but I think you want the PLine entry to come before the data???? Was this information in the original file? Can you just read it from there while creating this MIF file?

If you can't, then you will probably have to go down the array route, reading data into elements of an array until you get a line where the first two characters are less than the previous, then write out to file:
"PLine(" & UBound(sArrayVariable) & ")"
For I = LBound(sArrayVariable) to UBound(sArrayVariable)
Print #FileNumWrite, sArrayVariable(I)
Next I

Hope this helps,

Simon [sig][/sig]
 
Thanks - yes I do want to put the Pline (x) data before the start of the array. Can you possibly help me with the strucutre of the array, I have been reading, but am now a bit at a loss as to where to start.

cheers Lk [sig][/sig]
 
Try this:

Dim sLine As String
Dim FileNumRead As Integer
Dim FileNumWrite As Integer
Dim asData() As String
Dim lArrayCount As Long

Dim I As Integer

Dim sFileName As String
sFileName = App.Path & "\file.MIF" ' this is the file we are processing
Dim sNewFile As String
sNewFile = App.Path & "\new_file.MIF" ' this is the new file we are creating
FileNumRead = FreeFile

Open sFileName For Input As #FileNumRead
FileNumWrite = FreeFile
Open sNewFile For Output As #FileNumWrite

lArrayCount = 0

Do Until EOF(FileNumRead)
ReDim Preserve asData(lArrayCount + 1)
Line Input #FileNumRead, sLine ' get first line of data
If lArrayCount <> 0 Then ' not on the first entry for this block
If Val(Left(sLine, 2)) < Val(Left(asData(lArrayCount - 1), 2)) Then ' start of new block
Print #FileNumWrite, &quot;Pline(&quot; & lArrayCount & &quot;)&quot;
For I = 0 To lArrayCount - 1
Print #FileNumWrite, asData(I)
Next I
lArrayCount = 0
ReDim asData(1) ' removes old data from array (no Preserve)
asData(lArrayCount) = sLine
lArrayCount = lArrayCount + 1
Else
ReDim Preserve asData(lArrayCount + 1)
asData(lArrayCount) = sLine
lArrayCount = lArrayCount + 1
End If
Else
ReDim Preserve asData(lArrayCount + 1)
asData(lArrayCount) = sLine
lArrayCount = lArrayCount + 1
End If
Loop
' write out last block of data
Print #FileNumWrite, &quot;Pline(&quot; & lArrayCount & &quot;)&quot;
For I = 0 To lArrayCount - 1
Print #FileNumWrite, asData(I)
Next I

Close #FileNumRead
Close #FileNumWrite

MsgBox &quot;Completed Reading Tracks CRD (Ex_Track_crd) and Writing PLINE MIF (crd_mif)&quot;

I have checked this on the data:

21 1156 1157 2629974 6611014
22 1156 1157 2629979 6611013
22 1156 1157 2629980 6611013
23 1156 1157 2629993 6610897
21 1158 1159 2630065 6611130
23 1158 1159 2630080 6611298
21 1156 1157 2629974 6611014
22 1156 1157 2629979 6611013
23 1156 1157 2629993 6610897
21 1158 1159 2630065 6611130
23 1158 1159 2630080 6611298
21 1156 1157 2629974 6611014
22 1156 1157 2629979 6611013
22 1156 1157 2629980 6611013
23 1156 1157 2629993 6610897
21 1158 1159 2630065 6611130
23 1158 1159 2630080 6611298
21 1156 1157 2629974 6611014
22 1156 1157 2629979 6611013
23 1156 1157 2629993 6610897
21 1158 1159 2630065 6611130
23 1158 1159 2630080 6611298

and it produced another file of:

Pline(4)
21 1156 1157 2629974 6611014
22 1156 1157 2629979 6611013
22 1156 1157 2629980 6611013
23 1156 1157 2629993 6610897
Pline(2)
21 1158 1159 2630065 6611130
23 1158 1159 2630080 6611298
Pline(3)
21 1156 1157 2629974 6611014
22 1156 1157 2629979 6611013
23 1156 1157 2629993 6610897
Pline(2)
21 1158 1159 2630065 6611130
23 1158 1159 2630080 6611298
Pline(4)
21 1156 1157 2629974 6611014
22 1156 1157 2629979 6611013
22 1156 1157 2629980 6611013
23 1156 1157 2629993 6610897
Pline(2)
21 1158 1159 2630065 6611130
23 1158 1159 2630080 6611298
Pline(3)
21 1156 1157 2629974 6611014
22 1156 1157 2629979 6611013
23 1156 1157 2629993 6610897
Pline(2)
21 1158 1159 2630065 6611130
23 1158 1159 2630080 6611298

Simon
[sig][/sig]
 
As always, that was very informative and helpful
Thanks for all the time and effort put into it. I certainly learn more this way than any other. lk [sig][/sig]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top