EXAMPLE OF COMAL ON-LINE SUPPORT Subj: string func? From: Icarus Can someone explain strings and string functions, particularly placement of the colons? Subj: strings From: Captain C Since BASIC doesn't have user defined string functions, they are unfamiliar to most. Compare them to a built-in function. We talk about SPC$(5) returning 5 spaces: A$=SPC$(5) PRINT "HI",SPC$(5),"JIM" HI JIM See the 5 spaces in the printout. And A$ now is 5 spaces. Next we can write a string function: FUNC jim$(num) CLOSED DIM temp$ OF num*3 temp$="" // start with nothing FOR x=1 TO num DO temp$:+"JIM" RETURN temp$ ENDFUNC jim$ Now, try it: PRINT jim$(3) JIMJIMJIM Now, about the colons. Colons are used to specify substrings: A$="BIRTHDAY" A$(1:3) would be "BIR" A$(2:5) would be "IRTH" The colon just separates the start position from the end position. Subj: colons From: Comalite J Think of a string as a one dimensional array of characters: DIM mystring$ OF 6 At first, it looks like this: +---+---+ | 6 | --MAX (DIMmed Length) +---+---+ | 0 | --Current-Length +---+---+---+---+---+---+ | @ | @ | @ | @ | @ | @ | +---+---+---+---+---+---+ 1 2 3 4 5 6 Now execute this: mystring$:="Joel" +---+---+ | 6 | -- MAX-LEN +---+---+ | 4 | -- CUR-LEN +---+---+---+---+---+---+ | J | o | e | l | @ | @ | +---+---+---+---+---+---+ 1 2 3 4 5 6 A string's current length can never exceed the max length (specified in the DIM statement). Unlike a numeric array, a string can have an apparent current dimension that is smaller than its actual maximum dimension. Also unlike a numeric array, you can reference the entire string or any contiguous part of it containing 0 or more characters with indexing. Instead of a single number specifying which item of a numeric array to index, we use a RANGE to specify a SUBSTRING: mystring$(start : end) START and END are expressions which evaluate to a value between 1 and MAX-LEN. See the result here: PRINT mystring$(2:3) oe Characters 2 through 3 of mystring$ ("Joel"), output oe. The expression MYSTRING$(2:3) does this: +---+---+ | 6 | -- MAX-LEN +---+---+ | 4 | -- CUR-LEN +---+---+---+---+---+---+ | J | o | e | l | @ | @ | +---+---+---+---+---+---+ 1 2 3 4 5 6 ^ ^ Now type: mystring$:+" Rea" The ":+" assignment appends " Rea" onto the end of mystring$. The result is a string with the value "Joel Rea", but since mystring$ has a MAX-LEN of 6, characters after the 6th character are cut off. So the result is "Joel R". Thus: +---+---+ | 6 | -- MAX-LEN +---+---+ | 6 | -- CUR-LEN +---+---+---+---+---+---+ | J | o | e | l | | R | +---+---+---+---+---+---+ 1 2 3 4 5 6 Now type: PRINT mystring$(2:4) The result is "oel", characters 2 through 4 of the string "Joel R". If I just want character 3, I can specify the START and END to both be 3: PRINT mystring$(3:3) The result is "e". To reference a substring starting with the first character of a string, you normally specify a START of 1. COMAL has a shortcut: simply omit the START. Just specify the colon and the END. The next two lines are equivalent: PRINT mystring$(:4) PRINT mystring$(1:4) A similar shortcut is available for substrings which end with the last character. Just specify the first character followed by a colon. The next two lines are the same: PRINT mystring$(3:) PRINT mystring$(3:LEN(mystring$)) which, in this case, is: PRINT mystring$(3:6) The result is "el R". For a one character substring, just specify the START and END as the same: PRINT mystring$(3:3) e In COMAL, you can ASSIGN to a substring! For example, assuming mystring$ equals "Joel R", then: mystring$(2:4):="ack" PRINT mystring$ Jack R Characters 2-4 were changed without affecting the rest of the string. Substrings are padded with spaces to the right if needed: mystring$(2:4):="an" PRINT mystring$ Jan R mystring$(2:):="im V" PRINT mystring$ Jim V The above is an example of on-line COMAL support. In addition to message boards, there are upload / download libraries of COMAL programs and national monthly COMAL meetings: every first Sunday and second Thursday on QLink; every third Wednesday on People Link (and CIS pending). Meetings are at 9pm Central time. People Link - to get to our COMAL Support Section just /GO COMAL. To sign up on PLink call 1-800-524- 0100. EMail to CAPTAIN C. Quantum Link - to get to our COMAL Support Section go to CIN; Computing Support; Programmers Workshop. To sign up on QLink call 1-800-392-8200. EMail to CAPTAIN C. CompuServe (coming soon) - to get to our COMAL Support Section ... GO AMIGAV then LN to find COMAL listed. To sign up on CIS call 1-800-848-8199.