Results 1 to 10 of 10

Thread: help improve code -urgent

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2006
    Posts
    120

    help improve code -urgent

    Code:
    Private Sub Form_Load()
    Dim blnStartReading1 As Boolean, blnStartReading2 As Boolean
    Dim strTestData As String
    Dim A As Integer
    Dim B As Integer
    Dim i As Integer
    Dim ii As Integer
    Dim strLatitude As String
    Dim strLongitude As String
    Dim strDepth As String
    ChDir App.Path
    
    
    'Open strfilename For Input As #2
    Open "test.txt" For Input As #2
        blnStartReading1 = False: blnStartReading2 = False
        
        While Not EOF(2)
            Line Input #2, strTestData
            If strTestData = "STOP" Then blnStartReading2 = False
            
            If blnStartReading1 = True And blnStartReading2 = True Then
                A = Len(strTestData)
                For B = 1 To A
                    If Mid(strTestData, B, 1) = " " Then
                        strLongitude = Mid(strTestData, 1, B - 1)
                        strLatitude = Mid(strTestData, B + 1, A)
                        Exit For
                    End If
                Next B
                
            'MsgBox " " & strLatitude & " " & strLongitude & " " & strDepth & ""
            Open App.Path & "/Data.txt" For Append As #3
                Print #3, " " & strLatitude & " " & strLongitude & " " & strDepth & ""
            Close #3
    
           End If
                         
            If blnStartReading1 = True And blnStartReading2 = False Then
                i = InStr(strTestData, "174=")
                ii = InStr(i + 1, strTestData, ";")
                    If i < ii And i <> 0 Then
                        strDepth = Mid(strTestData, i, ii - i)
                    End If
                blnStartReading2 = True
            End If
          
          If strTestData = "EndHeader" Then blnStartReading1 = True
          If strTestData = "STOP" Then blnStartReading2 = False
          
        Wend
    Close #2
    End Sub
    the code above use to retrieve data from file "test.txt" and then rewrite it to file "data.txt" in another kind of format like below.
    3.85663000 100.81888000 174=0
    3.85625000 100.81778000 174=0
    3.85519000 100.80883000 174=0
    3.20013000 101.29401000 174=20
    3.20232000 101.29359000 174=20
    3.20748000 101.29134000 174=20

    i want stop to rewrite the data when the line context in file "test.txt" is "Start 302"
    Attached Files Attached Files

  2. #2
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: help improve code -urgent

    Code:
    Option Explicit
    
    Private Sub Form_Load()
    Dim intFF As Integer
    Dim strAll() As String
    Dim strItm() As String
    Dim strSwp() As String
    
    Dim lngIdxAll As Long
    Dim lngIdxItm As Long
    
       intFF = FreeFile
       Open "C:\test.txt" For Input As #intFF
          strAll = Split(Input(LOF(intFF), #intFF) & vbCrLf, "START ")
          'vbCrLf appended so last STOP also has vbCrLf like other STOP lines
       Close #intFF
       
       strAll(0) = vbNullString   'remove header
       For lngIdxAll = 1 To UBound(strAll)
          If Val(strAll(lngIdxAll)) = 302 Then
             ReDim Preserve strAll(lngIdxAll - 1)   'remove lines starting at START 302
             Exit For
          End If
          
          strItm = Split(strAll(lngIdxAll), vbCrLf)
          lngIdxItm = InStrRev(strItm(0), " ")
          strItm(0) = Mid$(strItm(0), lngIdxItm, InStr(lngIdxItm, strItm(0), ";") - lngIdxItm)
          For lngIdxItm = 1 To UBound(strItm) - 2   'exclude first line, STOP line and trailing ""
             strSwp = Split(strItm(lngIdxItm), " ")
             strItm(lngIdxItm) = strSwp(1) & " " & strSwp(0) & strItm(0) & vbCrLf
             'append vbCrLf cause Join() delimiter will be vbNullString
          Next
          strItm(0) = vbNullString   'remove first line
          strItm(UBound(strItm) - 1) = vbNullString 'remove STOP line
          strAll(lngIdxAll) = Join(strItm, vbNullString)
       Next
       
       Debug.Print Join(strAll, vbNullString)
       intFF = FreeFile
       Open "C:\testout.txt" For Output As #intFF
          Print #intFF, Join(strAll, vbNullString)
       Close #intFF
    End Sub

  3. #3

    Thread Starter
    Lively Member
    Join Date
    May 2006
    Posts
    120

    Re: help improve code -urgent

    leinad31,thanks for yr code.
    But, i still got problem because some my original file format is like below

    START 43, L, 174=0;133=300000
    START 43, L, 133=250000;174=5
    START 43, L, 174=10;133=300000
    START 43, L, 174=20;133=300000
    START 43, L, 133=250000;174=30
    START 43, L, 174=35;133=300000;102="Approximate depth contour"
    START 43, L, 133=250000;174=50;102="Approximate depth contour"
    START 43, L, 174=100;133=300000


    it is not only in format "START 43, L, 174=20;133=300000"
    So,for yr code will come error if in the data in format(blue color)

    for example:
    START 43, L, 133=300000;174=20
    101.29401000 3.20013000
    101.29359000 3.20232000
    101.29134000 3.20748000
    STOP

    the answer what i get by using yr code is
    101.29401000 3.20013000 133=300000
    101.29359000 3.20232000 133=300000
    101.29134000 3.20748000 133=300000

    But , what i needed is in
    101.29401000 3.20013000 174=20
    101.29359000 3.20232000 174=20
    101.29134000 3.20748000 174=20

  4. #4
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: help improve code -urgent

    Oh 174 is a label. Try replacing

    Code:
    lngIdxItm = InStrRev(strItm(0), " ")
          strItm(0) = Mid$(strItm(0), lngIdxItm, InStr(lngIdxItm, strItm(0), ";") - lngIdxItm)
    that comes before loop with
    Code:
    strItm(0) = " 174=" & Val(Mid$(strItm(0), InStr(1, strItm(0), "174=")+4))
    Last edited by leinad31; Mar 8th, 2007 at 01:23 PM.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    May 2006
    Posts
    120

    Re: help improve code -urgent

    leinad31, thank you very much.

    Now,i need to rearrange the data in file "data.txt"

    i want the data value arrange from small to big.

    from example:
    original:
    3.85663000 100.81888000 174=0
    3.85625000 100.81778000 174=0
    3.85519000 100.80883000 174=0

    after arrange:
    3.85519000 100.80883000 174=0
    3.85625000 100.81778000 174=0
    3.85663000 100.81888000 174=0

    how to write the code?

  6. #6
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: help improve code -urgent

    Before output to textout.txt recreate array with vbCrLf as delimiter

    strAll = Split(Join(strAll, vbNullString), vbCrLf)

    And transfer to a listbox with sorted = true

    Code:
    For lngIdxAll = 0 To Ubound(strAll)
       List1.AddItem strAll(lngIdxAll)
    Next
    Then for output.txt, simply iterate through sorted listbox and Print #intFF each listbox item.
    Last edited by leinad31; Mar 8th, 2007 at 08:58 PM.

  7. #7

    Thread Starter
    Lively Member
    Join Date
    May 2006
    Posts
    120

    Re: help improve code -urgent

    Quote Originally Posted by leinad31
    Before output to textout.txt recreate array with vbCrLf as delimiter

    strAll = Split(Join(strAll, vbNullString), vbCrLf)

    And transfer to a listbox with sorted = true

    Code:
    For lngIdxAll = 0 To Ubound(strAll)
       List1.AddItem strAll(lngIdxAll)
    Next
    Then for output.txt, simply iterate through sorted listbox and Print #intFF each listbox item.
    is it must use listbox? if i don't want using listbox, how to do it? thanks.

  8. #8
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: help improve code -urgent

    Then you will have to implement a sorting algorithm (eg. bubble, quicksort, etc). Do a search for samples to implement those.

  9. #9

    Thread Starter
    Lively Member
    Join Date
    May 2006
    Posts
    120

    Re: help improve code -urgent

    for the file "text.txt", i also need to get value in line2 to textbox1, value in line3 to textbox2,value in line4 in textbox3,value in line5 in textbox4. How to write the code by added to the code provide by leinad31 in #2.


    2.85667000 - textbox1
    99.70000000 -textbox2
    4.01666940 -textbox3
    101.46667000-textbox4

  10. #10
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: help improve code -urgent

    Update this line (the one before the outer for loop)

    Code:
    strAll(0) = vbNullString   'remove header
    with

    Code:
    strItm = Split(strAll(0), vbCrLf)  'transfer each line of header into an array element
    Text1.Text = strItm(1)
    Text2.Text = strItm(2) 
    Text3.Text = strItm(3)
    Text4.Text = strItm(4)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width