Two White Space Parsing Techniques

Started by ablincoe, March 19, 2014, 10:30:50 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

ablincoe

At times, I find a need to parse human entered data that is "visually pleasing", and not always the way a machine might like to see it.  To accommodate such data, I have needed a somewhat more generic "white space" parser than what DesignCAD has provided with the "PARSE" command.  Below are 2 subroutines that may help those with a similar need.  Both consider "white space" to be one (1) OR MORE space (and/or control character(s) (such as a TAB)), and trim (remove) "white space" before the first data as well.

Good Luck


PARSE_WHITE:               '  A PARSER of  -  A$
  B$ = ""               '    CLEAR Destination areas
  C$ = ""
  D$ = ""
  M_IDX = 1               '  Where to start within  -  A$
  M_L   = LEN(A$)            '  Where must I stop

  DO WHILE  MID$(A$,M_IDX,1) <= " " AND M_IDX < M_L   '    SKIP WHITE SPace
    M_IDX = M_IDX + 1
    LOOP
  DO WHILE  MID$(A$,M_IDX,1) > " " AND M_IDX < M_L   '    GET FIRST Value
    B$ = B$ + MID$(A$,M_IDX,1)            '   SAVE each char
    M_IDX = M_IDX + 1
    LOOP
  DO WHILE  MID$(A$,M_IDX,1) <= " " AND M_IDX < M_L   '    SKIP WHITE Space
    M_IDX = M_IDX + 1
    LOOP
  DO WHILE  MID$(A$,M_IDX,1) > " " AND M_IDX < M_L   '    GET NEXT Value
    C$ = C$ + MID$(A$,M_IDX,1)            '   SAVE each char
    M_IDX = M_IDX + 1
    LOOP
  DO WHILE  MID$(A$,M_IDX,1) <= " " AND M_IDX < M_L   '    SKIP WHITE Space
    M_IDX = M_IDX + 1
    LOOP
  DO WHILE  MID$(A$,M_IDX,1) > " " AND M_IDX < M_L   '    GET NEXT Value
    D$ = D$ + MID$(A$,M_IDX,1)            '   SAVE each char
    M_IDX = M_IDX + 1
    LOOP

' DO WHILE  MID$(A$,M_IDX,1) <= " " AND M_IDX < M_L   '    SKIP WHITE Space
'   M_IDX = M_IDX + 1
'   LOOP
' A$ = MID$(A$,M_IDX,999)            '  REMOVE ALREADY PARSED

RETURN

DIM     SPLIT$(100)
SPLIT_WHITE:
  FOR M_IDX = 1 to 100            '  CLEAR DESTINATIONs
    SPLIT$(M_IDX) = ""            '   One at a time
    NEXT I
  M_IDX = 1               '  Where to start within  -  A$
  M_L   = LEN(A$)            '  Where must I stop
  M_VAR = 1               '  Index for Array Offset

  DO WHILE  M_IDX <= M_L            '  START THE PARSING
    M_CH$ = MID$(A$,M_IDX,1)         '    My (next) Character
    IF  SPLIT$(M_VAR) <> ""  AND  _      '  TIME for the NEXT entry ?
   M_CH$ <= " "  THEN
      M_VAR = M_VAR + 1            '    INCREMENT Index Pointer
      IF M_VAR = 100  THEN         '    LAST VARIABLE available
   SPLIT$(M_VAR) = MID$(A$,M_IDX,9999)   '   SAVE THE REST here
   M_CH = " "            '   Clear CHAR Buffer
   M_IDX = M_L            '   Set END OF INPUT
   END IF
      END IF
    IF  M_CH$ > " "  THEN         '  A character needs saving
      SPLIT$(M_VAR) = SPLIT$(M_VAR) + M_CH$   '    SAVE the character at IDX
      END IF
    M_IDX = M_IDX + 1            '    Move on down the string
    LOOP

RETURN


[Edit]:  A 1-line correction has been made above.  Details:
     Old:     DO WHILE  M_IDX < M_L                         (misses the last character of the string)
     New:    DO WHILE  M_IDX <= M_L
[Edit]:  Inserted a "DIM" statement before the  -  SPLIT_WHITE:  -  routine.
           BasicCAD's processing if you forget this step can be quite bizarre!
Modify message

« Last Edit: March 19, 2014, 05:17:14 PM by ablincoe »

Dr PR

I have always been amazed at the failure of programmers to accomodate ordinary grammatical features like whitespace. Way back in 1970s pre-DOS CP/M days the command line parsers would work with any type and combination of whitespace (spaces, tabs, line ends, carriage returns, etc.). Modern programmers seem to lack the intelligence early programmers had.

The problem primarily is languages like C/C++ that were written by functional illiterates who had never written anything in natural language longer than their own names. They simply lacked the understanding of language to build in "common sense" text parsers, and now their ignorance has become institutionalized!

Phil
DesignCAD user since 1987