Results 1 to 4 of 4

Thread: Parsing of file

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2006
    Posts
    5

    Parsing of file

    Hi,
    I'm trying to parse out some information from a file. I want to get everything between "D 12" and "D 13".

    When I've managed that I want to get out data one after one. The format is given below:

    Code:
    ...
    lots of text
    ...
     D 12  Final trend cycle
      From  2002.Jan to 2005.Dec
      Observations         48
      Trend filter    23-term Henderson moving average
      I/C ratio        6.20
     -----------------------------------------------------------------------------
                   Jan      Feb      Mar      Apr      May      Jun 
                   Jul      Aug      Sep      Oct      Nov      Dec        TOTAL  
     -----------------------------------------------------------------------------
      2002          49.      52.      54.      57.      60.      63.
                    66.      68.      71.      74.      76.      77.          766.
     
      2003          78.      78.      78.      78.      78.      77.
                    77.      76.      76.      77.      78.      79.          931.
     
      2004          80.      81.      82.      83.      84.      85.
                    86.      87.      88.      88.      88.      89.         1020.
     
      2005          89.      90.      91.      92.      93.      94.
                    95.      96.      96.      96.      96.      96.         1125.
     
      AVGE          74.      75.      76.      78.      79.      80.
                    81.      82.      83.      84.      85.      85.
    
      Table Total-      3842.39   Mean-      80.05   Std. Dev.-      12.06
                                  Min -      48.61        Max -      96.39
            ICMETI,  Total Inventories Communications Equipment            PAGE  10, SERIES foo
    
     D 13  Final irregular component
    ...
    lots of text
    ...
    I've tried this to get data:
    Code:
    Sub Command1_Click() 
        text1 = "" 
        Dim tempvar As String 
        Open "I:\Jørgen\diverse\test\FOO.OUT" For Input As #1 
        Do While Not EOF(1) And LCase(tempvar) <> " D 12" 
            Input #1, tempvar 
        Loop 
        If Not EOF(1) Then 
            Do While Not EOF(1) And LCase(tempvar) <> " D 13" 
                Input #1, tempvar 
                If LCase(tempvar) <> "stop" Then 
                    text1 = text1 + tempvar + Chr(13) + Chr(10) 
                End If 
            Loop 
        End If 
        Close #1 
        Debug.Print text1 
    End Sub
    but without success.. If I input som other strings into the Sub like f.ex "start" and "stop" and also enter those data into the file I'm parsing it works OK. But I really need to do it without manually entering "start" and "stop" into the file..

    Any suggestions?

    Thanks in advance for any help!

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Parsing of file

    VB Code:
    1. f1 = FreeFile
    2. Open "I:\Jørgen\diverse\test\FOO.OUT" For Input As f1
    3.     strfi = Input(LOF(f1), #f1)
    4.     Close f1
    5.    
    6. pos = InStr(1, strfi, "D 12")
    7. pos1 = InStr(pos + 1, strfi, "D 13")
    8. mytest = Mid(strfi, pos, pos1 - pos)
    9. Debug.Print mytest

    give this a try to see if it does what you want


    this will put D12 in the string, but not D 13, just adjust you start and length in the Mid to get what you want


    pete
    Last edited by westconn1; Jul 10th, 2006 at 05:58 AM.

  3. #3

    Thread Starter
    New Member
    Join Date
    Jul 2006
    Posts
    5

    Re: Parsing of file

    It does!

    But then my problem is how to parse the data I've receivind into the String mytest.

    mytest is now equal to the following:
    Code:
    D 12  Final trend cycle
      From  2002.Jan to 2005.Dec
      Observations         48
      Trend filter    23-term Henderson moving average
      I/C ratio        6.20
     -----------------------------------------------------------------------------
                   Jan      Feb      Mar      Apr      May      Jun 
                   Jul      Aug      Sep      Oct      Nov      Dec        TOTAL  
     -----------------------------------------------------------------------------
      2002          49.      52.      54.      57.      60.      63.
                    66.      68.      71.      74.      76.      77.          766.
     
      2003          78.      78.      78.      78.      78.      77.
                    77.      76.      76.      77.      78.      79.          931.
     
      2004          80.      81.      82.      83.      84.      85.
                    86.      87.      88.      88.      88.      89.         1020.
     
      2005          89.      90.      91.      92.      93.      94.
                    95.      96.      96.      96.      96.      96.         1125.
     
      AVGE          74.      75.      76.      78.      79.      80.
                    81.      82.      83.      84.      85.      85.
    
      Table Total-      3842.39   Mean-      80.05   Std. Dev.-      12.06
                                  Min -      48.61        Max -      96.39
            ICMETI,  Total Inventories Communications Equipment            PAGE  10, SERIES foo
    I want to get all the values from each year, but not the total.
    That is a list with the values (49, 52, 54, 57, ...., 83, 84, 85, 85)

    Is this almost impossible? I can't think of a fast way to do this myself

  4. #4
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Parsing of file

    each array element of Data(x) will = all the data for that year... so in this case it would be like
    Data(0) = "49,52,54,57,60,63,66,68,71,74,76,77"

    VB Code:
    1. Dim Data() As String
    2.  
    3. Private Sub GetData()
    4. Dim tmp() As String
    5. Dim cnt As Integer
    6.     Open "C:\Path\to\File.txt" For Input As #1
    7.         tmp = Split(Input(LOF(1), 1), vbCrLf)
    8.     Close #1
    9.    
    10.    
    11.     cnt = 0
    12.     For x = 0 To UBound(tmp)
    13.         If IsNumeric(Left(tmp(x), 4)) Then
    14.             ReDim Preserve Data(cnt)
    15.             Data(cnt) = Mid(tmp(x), 15, 55)
    16.             Data(cnt) = Data(cnt) & Mid(tmp(x + 1), 15, 55)
    17.             Data(cnt) = Replace(Data(cnt), ".", ",") 'change . to ,
    18.             Data(cnt) = Replace(Data(cnt), " ", "") 'remove spaces
    19.             Data(cnt) = Left(Data(cnt),Len(Data(cnt))-1)
    20.             x = x + 1
    21.         End If
    22.     Next
    23. End Sub
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

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