Announcement

Collapse
No announcement yet.

PARSE$() techniques

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    PARSE$() techniques

    I am writing a little utility to convert data. I want to be able to use it on different formats. I have most of the code written to handle different fields (date and time formats etc).

    The thing that is giving me a little trouble is the first step, PARSEing out the various fields in a string:

    hFile = FREEFILE
    OPEN FilePathStr+FileNameStr FOR INPUT AS hFile LEN = 32768
    INPUT #hFile, InputStr ' Read a line of Data
    DateStr = PARSE$(InputStr, any ", ", 1)
    TimeStr = PARSE$(InputStr, any ", ", 2)

    Now this works for comma delimited fields like:
    19900118, 1610, 340.7
    or
    19900118,1610,340.7

    But I often get data with just spaces delimiting it:
    19900118 1610 340.7
    or
    19900118 1610 340.7
    or
    19900118 1610 340.7

    PARSEing on the space will only work if there is 1 space between each field. that is not allways the case

    So how do you PARSE out the fields without getting into a cumbersome loop and checking each character for a space etc etc

    Do you wizards have a neat little trick?


    ------------------
    Kind Regards
    Mike

    #2
    Mike,

    You can use the Replace function to make the 2 spaces = 1 space
    before doing the parse ... I had to do this once and it worked fine.
    I knew none of the data fields would have a space and that all fields
    would have a non-space character.

    Replace " " with " " in InputStr

    Mike




    ------------------

    Comment


      #3
      It sounds like you need a simple routine to "conform" the incoming data before you begin processing it...

      Code:
      REPLACE " " WITH "," IN sTarget$
      DO
          IF INSTR(sTarget$,",,") = 0 THEN EXIT LOOP
          REPLACE ",," WITH "," IN sTarget$
      LOOP
      You could change the first REPLACE to REPLACE ANY if you also wanted to support semicolons, etc.

      -- Eric


      ------------------
      Perfect Sync Development Tools
      Perfect Sync Web Site
      Contact Us: mailto:[email protected][email protected]</A>

      [This message has been edited by Eric Pearson (edited June 16, 2001).]
      "Not my circus, not my monkeys."

      Comment


        #4
        This is waht I am trying to avoid:

        hFile = FREEFILE
        OPEN FilePathStr+FileNameStr FOR INPUT AS hFile LEN = 32768 ' Open data file for reading
        'LINE INPUT #hFile, InputStr ' Discard first line of data
        INPUT #hFile, InputStr ' Read first line of Data
        IF InputStr = "" THEN EXIT FUNCTION ' end of data or a hole in data
        DateStr = PARSE$(InputStr, any ", ", 1)
        Done = 0
        Count = 2
        TimeStr = ""
        WHILE TimeStr = "" AND Done = 0 ' figure out spaces B4 Time
        TimeStr = PARSE$(InputStr, any ", ", Count)
        INCR Count
        DIALOG DOEVENTS : DIALOG DOEVENTS
        WEND
        TimeSpaces = Count - 1
        PriceStr = ""
        WHILE PriceStr = "" AND Done = 0 ' figure out spaces B4 Price
        PriceStr = PARSE$(InputStr, any ", ", Count)
        INCR Count
        DIALOG DOEVENTS : DIALOG DOEVENTS
        WEND
        PriceSpaces = Count - 1


        WHILE NOT EOF(hFile) AND Done = 0 ' go through the rest of the file
        INPUT #hFile, InputStr ' Read subsequent lines of data
        IF InputStr = "" THEN EXIT FUNCTION ' end of data or a hole in data
        DateStr = PARSE$(InputStr, any ", ", 1)
        TimeStr = PARSE$(InputStr, any ", ", TimeSpaces)
        PriceStr = PARSE$(InputStr, any ", ", PriceSpaces)
        Price = VAL(PriceStr)
        'Process Date, Time Price
        WEND



        ------------------
        Kind Regards
        Mike

        Comment


          #5
          If you use the code I posted to process your InputStr string, you can simplify that code a lot!

          It will produce a string that can be used with PARSECOUNT and PARSE$ very easily. Nothing but data separated by commas.

          -- Eric

          ------------------
          Perfect Sync Development Tools
          Perfect Sync Web Site
          Contact Us: mailto:[email protected][email protected]</A>
          "Not my circus, not my monkeys."

          Comment


            #6
            Mike,

            You must know something about the data, if you know it is always a date
            followed by a number followed by say an email address then you have some
            knowledge about what needs to be taken out. If the only thing you are
            unsure about is the delimiter and you do not want to use the technique
            that Eric suggested then you can look at regular expressions. Take a look
            at REGEXP and REGREP see if you can do something with that. Unless the
            data is quite complex though, you would be better off following Eric's advice.

            Regards
            Trevor

            ------------------

            Comment


              #7
              Perfect!
              Thank you Mike, Eric and Trevor. I learned two new functions that are exactly what I need.

              This forum is so cool!

              ------------------
              Kind Regards
              Mike

              Comment

              Working...
              X
              😀
              🥰
              🤢
              😎
              😡
              👍
              👎