Previous | Contents | Index |
The TRIM$(), LTRIM$(), RTRIM$() and EDIT$() functions can be used to remove spaces, tabs and other characters. SPACE$() can be used to insert spaces.
This example shows how to use the trim and edit functions.
1 program application_8_3 10 clear print at 1, 1:; 20 dim text$(8,2) text$(1,1) = ' TRIM$ discards leading and trailing spaces and t text$(1,2) = trim$(text$(1,1)) text$(2,1) = ' LTRIM$ discards LEADING spaces and tabs ' text$(2,2) = ltrim$(text$(2,1)) text$(3,1) = ' RTRIM$ discards TRAILING spaces and tabs ' text$(3,2) = rtrim$(text$(3,1)) text$(4,1) = ' E D I T - 2 - discards all spaces and tabs ' text$(4,2) = edit$(text$(4,1), 2) text$(5,1) = 'E D I T - 16 - reduces spaces, tabs to 1 sp text$(5,2) = edit$(text$(5,1), 16) text$(6,1) = 'edit - 32 - converts lower case to upper case' text$(6,2) = edit$(text$(6,1), 32) text$(7,1) = ' e d i t - 3 4 - discards spaces, tabs & upper cases ' text$(7,2) = edit$(text$(7,1), 34) text$(8,1) = 'SPACE$ creates spaces' text$(8,2) = space$(5) + text$(8,1) + space$(5) 30 for i = 1 to 8 print '|'; text$(i,1); '|' print '|'; text$(i,2); '|' print delay next i 40 end |
| TRIM$ discards leading and trailing spaces and tabs | |TRIM$ discards leading and trailing spaces and tabs| | LTRIM$ discards LEADING spaces and tabs | |LTRIM$ discards LEADING spaces and tabs | | RTRIM$ discards TRAILING spaces and tabs | | RTRIM$ discards TRAILING spaces and tabs| | E D I T - 2 - discards all spaces and tabs | |EDIT-2-discardsallspacesandtabs| |E D I T - 16 - reduces spaces, tabs to 1 space| |E D I T - 16 - reduces spaces, tabs to 1 space| |edit - 32 - converts lower case to upper case| |EDIT - 32 - CONVERTS LOWER CASE TO UPPER CASE| | e d i t - 3 4 - discards spaces, tabs & upper cases | |EDIT-34-DISCARDSSPACES,TABS,UPPERCASES| |SPACE$ creates spaces| | SPACE$ creates spaces | |
The following is a list of the EDIT$ operations that can be performed. You can also combine edit values to perform more than one operation. For example, edit-34 in the above example, consists of edit value 2 (discard all spaces and tabs) plus edit value 32 (convert lower case to upper case).
Value | Edit Operation |
---|---|
1 | Trim parity bits. |
2 | Discard all spaces and tabs. |
4 | Discard characters: CR, LF, FF, ESC, RUBOUT, and NULL. |
8 | Discard leading spaces and tabs. |
16 | Reduce spaces and tabs to one space. |
32 | Convert lower case to upper case. |
64 | Convert "[" to "(" and "]" to ")". |
128 | Discard trailing spaces and tabs. |
256 | Do not alter characters inside quotes. |
8.3 Extracting Data
This section explains how to extract pieces of data from a text string
or data field.
ELEMENT$() | allows you to extract a specific item from a list of items; the items can be separated by a comma (the default), space, etc. | |
MID or MID$() | allows you to extract characters from within a text string or data field; you must provide the start position and, optionally, the end position | |
SEG$() | works the same as MID except that you must provide both the start and end positions of the data you want to extract | |
POS() | gives you the starting position of specified data in a text string or data field | |
LEN() | tells you how long a text string or field is | |
PATTERN() | gives you the starting position of a specific pattern of characters that you are looking for or tells you if the data is present in the text string or field; in the following example, we are looking for "arrive ??"; the "??" means that any 2 data characters following "arrive " are acceptable | |
REPEAT$() | creates a character the number of times you specify |
All of the above functions and many more are described in the appendix section of the INTOUCH - A Guide to the Language manual. |
The following example shows you how to use functions to extract data.
1 program application_8_4 10 clear print at 1, 1:; dim line$(3), carrier$(3), flight_no$(3), time$(3) 20 line$(1) = 'United Flight #452 is scheduled to arrive at 8:15am.' line$(2) = 'American Flight #220 is scheduled to arrive at 9:44am.' line$(3) = 'TWA Flight #33 is delayed and will arrive at 7:30pm.' 30 print 'Specific data is extracted from these text lines:' print for i = 1 to 3 print space$(5); line$(i) next i print print 40 for i = 1 to 3 carrier$(i) = element$(line$(i), 1, ' ') flight_no$(i) = mid$(line$(i), pos(line$(i), '#')+1, len(line$(i))) flight_no$(i) = element$(flight_no$(i), 1, ' ') time$(i) = seg$(line$(i), pattern(line$(i), 'arrive ??')+10, len(line$(i))-1) next i 50 print 'Carrier', 'Flight#', 'Arrival' z$ = repeat$('-', 7) print z$, z$, z$ for i = 1 to 3 print carrier$(i), flight_no$(i), time$(i) next i 60 end |
Specific data is extracted from these text lines: United Flight #452 is scheduled to arrive at 8:15am. American Flight #220 is scheduled to arrive at 9:44am. TWA Flight #33 is delayed and will arrive at 7:30pm. Carrier Flight# Arrival ------- ------- ------- United 452 8:15am American 220 9:44am TWA 33 7:30pm |
If you need to extract characters from the end of a text string or data field, you can use the RIGHT$() function. This function will give you the rightmost characters. You specify the number of characters you want.
When you need to work with parts of a text string, you can use either the ELEMENT$() or PIECE$() function to break it down into pieces. If you want to know the number of pieces contained in the text string, you can use either the ELEMENT() or PIECE() function to tell you.
ELEMENT() and ELEMENT$() automatically trim leading and trailing spaces, skip over quoted data and set the default separator to a comma. The default separator for PIECE() and PIECES$() is the carriage return/line feed sequence.
?15pc
1 program application_8_5 10 clear text1$ = 'Some Animals in the Zoo' text2$ = 'Bear, Elephant, Zebra, Tiger, Lion, Monkey, Giraffe, Gorilla' 20 print at 3,20: text1$ print at 5,5: text2$ print print 30 z = elements(text2$, ' ') print 'There are'; z; 'elements in the animal list' print 'The 5th element is: '; element$(text2$, 5, ', ') print 40 z = pieces(text2$, ' ') print 'There are'; z; 'pieces in the animal list' print 'The 5th piece is: '; piece$(text2$, 5, ', ') 50 end |
Some Animals in the Zoo Bear, Elephant, Zebra, Tiger, Lion, Monkey, Giraffe, Gorilla There are 8 elements in the animal list The 5th element is: Lion There are 8 pieces in the animal list The 5th piece is: Lion |
8.4 Changing/Replacing Characters/Strings
You can change and replace data using the CHANGE$()
and REPLACE$() functions.
?15pc
1 program application_8_6 10 clear print at 1, 1:; 20 print at 3,5: 'This example CHANGEs "at" to "x":' text$ = 'My cat is fat' old$ = 'at' new$ = 'x' print at 5,10: 'Before: '; text$ print at 6,10: 'After : '; change$(text$, old$, new$) 30 print at 9,5: 'This example REPLACEs "og" and "as" with "yyy":' text$ = 'My dog has fleas' old_new$ = '{<og|as>}=yyy' print at 11,10: 'Before: '; text$ print at 12,10: 'After : '; replace$(text$, old_new$) 40 end |
This example CHANGEs "at" to "x": Before: My cat is fat After : My cxx is fxx This example REPLACEs "og" and "as" with "yyy": Before: My dog has fleas After : My dyyy hyyy fleyyy |
8.5 Converting Data
It is necessary to convert numeric values to string values and string
values to numeric values to perform certain types of processing. For
example, some formatting can only be done on string values and
calculations can only be done on numeric values.
INTOUCH has several functions you can use to convert data.
a = 25 z$ = str$(a) |
a$ = "25" z = val(a$) |
ucase$(text$) |
lcase$(text$) |
quote$(text$) |
unquote$(text$) |
Writing code which has clear logic flow is essential to effective programming. It helps you and others understand the program's goals, thus minimizing time and effort spent on future changes and enhancements.
INTOUCH offers an array of conditionals, loop constructs and branching statements. Each type of statement targets different areas of logic flow.
9.1 Conditional Statements
Programmers often need to check for specific conditions and take some
type of action accordingly. INTOUCH provides two types of conditional
constructs (statements). The types are:
Which of these two constructs to use depends on the condition that is being checked.
The IF/THEN construct is the simplest conditional construct and is most often used for basic checks (i.e. if _EXIT then exit do).
The SELECT CASE construct is most often used when the condition can be a number of elements of the same type (i.e. ADD,CHA,DEL,INQ). The SELECT CASE construct is much like a specialized IF/THEN. It solely relies on one expression, mapping a different path for each possible match.
Therefore, it is advantageous to use the SELECT CASE statement instead of the IF/THEN when you are dealing with one expression which has more than just a few possible matches.
The following example shows some conditions when the IF/THEN and SELECT CASE statements might be used.
1 program application_9_1 10 procedures$ = '%at 2, 10, %title "Procedures",' & + '"Add New Customers" = ADD, "Change Customer Data" = CHA,' & + '"Delete Customers" = DEL, "Customer Inquiry" = INQ, "Exit"' 20 last_option$ = '' 30 do clear line input menu procedures$, default last_option$: selection$ if _back or _exit then exit do last_option$ = _string print at 1, 1:; select case selection$ case 'ADD' print 'Adding new customers...' case 'CHA' print 'Changing customer data...' case 'DEL' print 'Deleting customers...' case 'INQ' print 'Customer inquiries...' end select delay loop 40 message 'The End' 50 end |
+-------Procedures--------+ | Add New Customers | | Change Customer Data | | Delete Customers | | Customer Inquiry | | Exit | +-------------------------+ |
The following is displayed if "Change Customer Data" is selected.
Changing customer data... Press the RETURN key to continue |
The menu defaults to the previous selection.
+-------Procedures--------+ | Add New Customers | | Change Customer Data | | Delete Customers | | Customer Inquiry | | Exit | +-------------------------+ |
When you create a program and routines in a program, you want the logic of the program and the routines to flow downward. This flow can be achieved by using loops. There are several different types of loops.
A loop performs a set of instructions over and over. However, there are times when, under certain conditions, you want to branch around in the loop or you want to exit out of the loop. Depending on which type of loop you are in, there are different ways of branching and exiting.
If you are in an INTOUCH loop and want to jump back to the beginning of the loop without executing remaining lines in the loop (and possibly incrementing an index variable), you can use the REPEAT statement. The REPEAT FOR statement is used for FOR/NEXT loops, and the REPEAT DO statement is used for the DO/LOOP and for the DO/END DO loops.
If you are in a loop and want to jump to the next iteration without executing the rest of the code within the loop, you can use the ITERATE statement. The ITERATE FOR statement is used for FOR/NEXT loops, and the ITERATE DO statement is used for DO/LOOP and DO/END DO loops.
If you are in a loop and want to exit, you can use the EXIT statement. The EXIT FOR statement is used for FOR/NEXT, loops and the EXIT DO statement is used for DO/LOOP and DO/END DO loops.
1 program application_9_2 10 do clear print at 1, 1:; count = 0 answer$ = '' password$ = 'PASSWORD' do while answer$ <> password$ input screen 'Password: <ucase, noecho:@@@@@@@@>', at 3,1: answer$ if _back or _exit then exit do count = count + 1 loop until count = 3 print if answer$ = password$ then print 'Login complete' else print 'Login failed' message delay: 'Sorry about that. Try this word "password"' repeat do end if 20 end do 30 end |
Password: ________ Login complete |
9.3 Routines
Programs normally consist of a series of routines. A routine is a
self-contained block of code which performs a task. Routines are
convenient to reference, and they can make the main body of the program
short and simple.
You can easily create routines in INTOUCH. The following is a list of recommended guidelines to use when writing program routines.
The INTOUCH Editor has many helpful aids to assist you in writing programs. For example, a simple command will create a routine header outline and you need only fill in the appropriate information. The chapter, "Working in the INTOUCH Environment", explains how to use some of the INTOUCH Editor's program writing tools. |
Example 9-1 Example of a Routine |
---|
16500 !%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% ! G E T P R E V S C R E E N N U M B E R !%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% ! ! Brief description: ! Determine the number of the previous screen. If this is the ! first screen, display an error message. ! ! Expected: ! scrn_nbr% = current screen number ! ! Locals: ! error = TRUE if screen number is first screen ! ! Results: ! scrn_nbr% = previous screen number to display ! !%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% routine get_prev_screen_number error = false z1% = scrn_nbr% - 1 if z1% < 1 then error = true message error : "This is the first screen" else scrn_nbr% = z1% end if end routine |
Previous | Next | Contents | Index |