Complex file parsing

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • davebaty
    New Member
    • Apr 2007
    • 5

    Complex file parsing

    I'm relatively new to VB programming (VB 2005), and have come across a problem parsing complex text files. Basically I have a file which has lines something like the following:

    [WEIGHT_AND_BALA NCE]
    max_gross_weigh t = 150000 // (pounds)
    empty_weight = 74170 // (pounds)

    max_number_of_s tations = 50

    station_load.0 = "170.0, 41.0, -1.5, 0.0, Pilot" //Weight (lbs),
    station_load.1 = "170.0, 41.0, 1.5, 0.0, Co-Pilot" //Weight (lbs),
    station_load.2 = "510.0, 0.0, 0.0, 0.0, Crew" //Weight (lbs),
    station_load.3 = "1360.0, 25.5, 0.0, 0.0, First Class" //Weight (lbs),

    What I need to do is search the file to find the 'empty_weight' line for example and find the value after the = sign. Similarly, with the station_load sections, I would need the first number after the quotations, as integers and disregard the rest of the line.

    I am not sure how to achieve this, I can do simple text parsing with a single delimiter, but don't really know where to start with much more complex files.

    Thanks.
  • Killer42
    Recognized Expert Expert
    • Oct 2006
    • 8429

    #2
    I'm not going to go into detail, because I work in VB6 and would possibly just confuse you. But I'd suggest all you need to do is read the text line by line. For each line, you check whether Left(Text, nn) = "your string". If so, then discard that part of the line. Look through the rest of the line, character by character until you hit something which is not a numeric digit (most likely using the Mid() function). Then just take everything before that point as your value.

    If you know there will always be, for example, a space after the number, then you can save time by using something like Instr() function to find the space.

    Comment

    • davebaty
      New Member
      • Apr 2007
      • 5

      #3
      Thanks. Sounds easy enough, I will try it out.

      Comment

      • Killer42
        Recognized Expert Expert
        • Oct 2006
        • 8429

        #4
        Originally posted by davebaty
        Thanks. Sounds easy enough, I will try it out.
        Let us know how it goes.

        Comment

        Working...