DesignCAD 3D Max 2027 box
Now available

DesignCAD 2027

Hatches that follow their boundaries, an on-screen dial to turn your model, and a way to find any command by name, in a clean Windows 11 look.

Rule for parsing space-delimited strings

Started by bdeck, September 11, 2008, 05:07:47 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

bdeck

There has been some recent discussion regarding the problems caused by expression evaluation in the input # command, and the inability to input space-delimited fields.

DT has pointed out that the Parse command may be used to process space-delimited lines read from text files using the Input # command.

(NOTE: automatic expression evaluation in the input# statement was eliminated in version 19)

If the input # command is used to read an entire line into a single string variable, evaluation is suppressed. That string may then be parsed into individual string or numeric variables.

RULE FOR PARSING

If a character-delimited field with embedded spaces is included in a space-delimited source string,
It must be the last field in the string.



If this rule is not followed, the first parsed variable following a character-delimited field may be ignored.
There are obvious hacks around this limitation, but they will likely break when this bug is fixed.

BD




'Parsedemo.d3m    demonstration of Rule for parsing space-delimited lines    bdeck 09/08

'Parse can be used to process space delimited lines from text files.

'If a character-delimited field with embedded spaces is included in a space delimited source string,
' It must be the last field in the string.

'If this rule is not followed, the first parsed variable following a character-delimited field may be ignored.


'q$=chr$(34)   ' Quote mark
'q$="^"
q$="'"       ' First choose a delimiter for text string with embedded spaces

a=-5
a$="a"
b=-7
aaa$="a b c d"
aa$=q$+aaa$+q$            'create character-delimited field

'************* Space delimited files *************

open "o", 1, "test.dat"
print #1,a,a$,b,aa$          'delimited field must be last field on line.
close

open "i", 1, "test.dat"
input #1 xx$                 ' a single string input reads the entire line
close 1

print "xx$", xx$
anykey

fm$="%s %s %s "+q$+"%s"+q$    'parse to all string variables
parse xx$,fm$,b1$,b2$,b3$,b4$ 
print "parsed to all string vars   ", fm$,"   ",b1$,b2$,b3$,b4$
anykey

fm$="%d %s %d "+q$+"%s"+q$    'parse to mixed string and numeric vars
parse xx$,fm$,b1,b2$,b3,b4$ 
print "parsed to mixed string and numerics   ", fm$,"   ",b1,b2$,b3,b4$ 
anykey

bdeck

Another good rule (particulaly if a string is likely to contain an expression) is to allways parse to string variables.

You then have the option of manipulating each resulting string or assigning it to a numeric variable.

An expression included in the source string will cause an error or incorrect value if parsed directly into a numeric variable. (whether delimited by spaces, commas, etc)


BD