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.

Rules for parsing comma delimited strings

Started by bdeck, September 12, 2008, 04:59:20 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

bdeck

Following up on rules for parsing space delimited strings, the following rules appear to apply to comma-delimited files.

Parsing is necessary because the automatic expression evaluation feature of input# virtually assures days of abject misery and heartache for the programmer.

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

RULES

1) To make certain the final argument of source sting is parsed, always add a terminal throwaway argument to the source string prior to parsing.

2) Format string must include a final delimiter in order to read the final argument in the original source string.

3) 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) The downside of this rule is that the miniscule type checking available in parse will be lost.







' parse demo for comma delimited strings

close


q$=chr$(34)   ' Quote mark
'q$="^"
'q$="'"       ' Note: shown for demo purpose only, not used as a delimiter, just part of the string 

a=-5
a$="a"
b=-7
c=-10
aaa$="a b c d"
aa$=q$+aaa$+q$


'************ Comma delimited files *****************

open "o", 1, "test.dat"
print #1, a,",",aaa$,",",b,",",aa$,",",c,"   
close

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

print "xx$ =   ", xx$
anykey x

xx$=xx$+",terminator"                          ' at least one extra delimited character must be added to source string

print "xx$ =   ", xx$
anykey x

fm$="%s,%s,%s,%s,%s,"                             'format string must include a final delimiter 
parse xx$,fm$, b1$,b2$,b3$,b4$,b5$
print "parsed   ", fm$,"   ",b1$,b2$,b3$,b4$,b5$  ' parse only to string variables, or risk insanity
anykey x


charly-bochum

Better can't be shown: Give us back the old input# of BasicCad or make the new one compatible with the old one, please!