Teach You, Teach Me (Part 3)
Back to Formatting, but
SCRN004Þ
;Program demonstrates the library function COLLIB - Feb 1997 -
ACB
Þ;M Web Magazine @
http://geocities.datacellar.net/SiliconValley/7041/mwm.html
Þ;You are using this program at your own risk
Þ;
ÞFOR
COL="WHITE","RED","BLUE","BLACK","MAGENTA","CYAN","GREEN","YELLOW"
D SETFGCOL^COLLIB(COL) W !!,"COLOR ME NOW"
ÞQUIT
MSM/Student does provide a mnemonic that sets foreground and background colors, but this mnemonic is incorrect. MWM has contacted Micronetics and we have been told that the correction will appear in the next release of MSM. Although we didnt have this mnemonic, we very soon created our own library for this type of formatting. It is called COLLIB and is listed below. This library has three procedure within it: SETBGCOL (set background color), SETFGCOL (set foreground color) and SETCOL. SETCOL takes two parameters specifying both the foreground and background colors. This library is listed below. All the procedures in this library refer to colors using their written name (in capitals at the moment).
COLLIBÞ; Color
Library - ACB - Feb 1997
Þ;M Web Magazine @
http://geocities.datacellar.net/SiliconValley/7041/mwm.html
Þ;You are using this program at your own risk
Þ;
SETFGCOL(COL)Þ
;Sets the foreground color to that passed as a parameter. If an
invalid color is specified the default will be white and a beep
will be sounded - ACB - Feb 1997
ÞN I,SCOL
ÞS I=0 FOR
SCOL="BLACK","RED","GREEN","YELLOW","BLUE","MAGENTA","CYAN","WHITE"
Q:SCOL=COL S I=I+1
ÞI SCOL'=COL W /BEL S I=7
ÞW *27,"[",(30+I),"m"
ÞQ
Þ;*** EOR ***
SETBGCOL(COL)Þ
;Sets the background color to that passed as a parameter. If an
invalid color is specified the default will be black and a beep
will be sounded - ACB - Feb 1997
ÞN I,SCOL
ÞS I=0 FOR
SCOL="BLACK","RED","GREEN","YELLOW","BLUE","MAGENTA","CYAN","WHITE"
Q:SCOL=COL S I=I+1
ÞI SCOL'=COL W /BEL S I=0
ÞW *27,"[",(40+I),"m"
ÞQ
Þ;*** EOR ***
SETCOL(FCOL,BCOL)Þ
;Calls SETFGCOL and SETBGCOL with the two colors passed as
parameters. Invalid entries are handled by the respective
procedures - ACB - Feb 1997
ÞD SETFGCOL(FCOL),SETBGCOL(BCOL)
ÞQ
SCRN005 also makes use of COLLIB. This should help drive home the basic idea of having a library function. Write it once use it many times from different programs.
Both COLLIB and SCRN005 introduce us to an new instruction; NEW or as it appears in our program N. In order to explain this command we shall first have to understand how variables operate in M.
The Life of Variable
The life of a variable defines how long the variable remains in existence after it is created. If youve been playing around with M for some time, youve probably already come across the error <UNDEF> whenever you try to manipulate a variable that doesnt exist.
Unlike other programming languages, a variable in M will last until M is shut down. To check this claim out, shutdown MSM and load it again. When you get the command prompt, type the command WRITE and you should get nothing. DO ^SCRN002 and after the program finishes, type WRITE again. What did you get? The value of variables as they were last set by SCRN002.
From this paragraph there are a couple of things to note:
- Typing WRITE gives you what variable are defined.
- Support for the claim made above.
This extended life can cause problems and a programmer must be aware of this. For example during a development session a program appears to work well, yet when it is tested elsewhere the same code gives errors. What these baffled people fail to notice is that during development certain variables are already defined.
We can solve this problem by using the command KILL or (K). This command takes three different varieties:
KILL<two spaces>
In this flavor KILL is kamikaze. It destroys all variables. Some programmers like to use this instruction at the beginning of their programs so as to ensure that nothing is defined upon commencement.
KILL <variable>, ,<variable>
Here KILL is specifically told to destroy only the variables specified. Other variables are not effected. One example for the use of such a command is to remove variables that hold sensitive data, once the data has been done with.
KILL (<variable>, ,<variable>)
Here the variables are enclosed in brackets. This type of instruction instructs KILL to remove all variables except those specified.
RTN006Þ
;Program demonstrates procedure calls - May 1997
Þ;M Web Magazine @
http://geocities.datacellar.net/SiliconValley/7041/mwm.html
Þ;You are using this program at your own risk
Þ;
ÞK
ÞF I=1:1:100 D CURRLINE(I)
ÞQ
Þ;*** EOR ***
CURRLINE(LINE)Þ
; Displays the current line
ÞW !,"This is line ",LINE,"
(Press CTRL+C to terminate program)"
ÞS I=LINE-1
ÞW !,"The Line before is ",I,!
ÞQ
Run the program shown here. Why is it that variable I never increments to two as instructed by the FOR command? Dont forget, press CTRL+C to stop the program. Dry run the program paying special attention to what values I takes when the program is running.
The problem is that I in SHOWLINE, I is being decremented by 1 and when control returns to MAIN it is initiating every time. This is a common problem in programming and the solution lies in encapsulating variables. Basically this means that although we have used I in the two modules we want the program to treat both separately.
By NEWing the variable I in CURRLINE we are instructing the computer to store the current value of I, creating a new instance of it within CURRLINE. As CURRLINE comes to an end, the variable I created within it is destroyed and the original value restored. RTN007 is the modified version of RTN006 (and it works well).
RTN007Þ
;Program demonstrates procedure calls - May 1997
Þ;M Web Magazine @
http://geocities.datacellar.net/SiliconValley/7041/mwm.html
Þ;You are using this program at your own risk
Þ;
ÞK
ÞF I=1:1:100 D CURRLINE(I)
ÞQ
Þ;*** EOR ***
CURRLINE(LINE)Þ
; Displays the current line
ÞN I
ÞW !,"This is line ",LINE,"
(Press CTRL+C to terminate program)"
ÞS I=LINE-1
ÞW !,"The Line before is ",I,!
ÞQ
Some of you might be wondering what all the fuss is about. Couldnt we have use J instead of I, or IA or something else? Definitely yes, but try to imagine a situation in which your program is a good few thousand lines long, calling three dozen library routines written by yourself and by others. Are you willing to memorize each and every variable? What happens if you didnt notice that a variable is already in use, is the error variable-related or is it something else? The solution simply doesnt hold in practice.
Also, we have observed that many of those who practice encapsulation normally prove to be better programmers in the long run. Since you must know what variables are being used inside the procedures, it normally encourages more planning than the quick and dirty approach of thinking and writing.
Before we conclude this section, one thing we would like to add is that parameters are automatically encapsulated in that they are automatically NEWed when the procedure becomes active.
What do you think will happen if you new a variable inside a procedure? Alter RTN007 and include a NEW I after the instruction S I=LINE-1.
In MWM001 we told you that practice makes perfect. We repeat this here. Take situations you are familiar with and write modular programs for them. The more time you put into this, the more proficient you will become.
SCRN005Þ
;Program demonstrates the library function COLLIB - Feb 1997 -
ACB
Þ;M Web Magazine @
http://geocities.datacellar.net/SiliconValley/7041/mwm.html
Þ;You are using this program at your own risk
Þ;
ÞDO SETBGCOL^COLLIB("BLACK")
ÞWRITE /ED(2),/CUP(1,32),"SAY IT IN
COLOR"
ÞREAD /CUP(5,1),"Enter your Colorful
Quote: ",QUOTE:30
Þ;Check if the user input has timedout
ÞQUIT:('$TEST)!(QUOTE="")
ÞWRITE /CUP(7,1),"Enter Foreground
Color"
ÞDO COLLIST SET FGCOL=COL
ÞWRITE /CUP(7,1),/ED(0),"Enter Background
Color"
ÞDO COLLIST SET BGCOL=COL
ÞDO SETCOL^COLLIB(FGCOL,BGCOL) W
/CUP(7,1),/ED(0),QUOTE
ÞREAD /CUP(20,1),"Press Any Key
",*ANS
ÞQUIT
Þ;*** EOR ***
COLLISTÞ
;Starting at Line 9 display the colors. The selected color is
stored in the variable COL. Background is set to Black, but the
color black is show on a white background so as to be visible.
ÞNEW I,CHOICE
ÞDO SETBGCOL^COLLIB("BLACK")
ÞWRITE /CUP(9,1)
ÞSET I=1
ÞFOR
COL="BLACK","RED","GREEN","YELLOW","BLUE","MAGENTA","CYAN","WHITE"
D SETFGCOL^COLLIB(COL) D SET I=I+1
Þ.D:I#2=1 SETBGCOL^COLLIB("WHITE")
Þ.D:I#2=0 SETBGCOL^COLLIB("BLACK")
Þ.W /EL(2),I,". ",COL,!
Þ.Q
A10ÞREAD
/CUP(20,1),"Enter Color: ",CHOICE
ÞI CHOICE=1 S COL="BLACK"
ÞE IF CHOICE=2 SET COL="RED"
ÞE IF CHOICE=3 SET COL="GREEN"
ÞE IF CHOICE=4 SET COL="YELLOW"
ÞE IF CHOICE=5 SET COL="BLUE"
ÞE IF CHOICE=6 SET COL="MAGENTA"
ÞE IF CHOICE=7 SET COL="CYAN"
ÞE IF CHOICE=8 SET COL="WHITE"
ÞE WRITE /BEL GO A10
ÞQUIT
DO is a very loaded command. In SCRN005 we discuss yet another method of how to group command. In label A10-5, we have a version of DO that will ultimately help us to uncompound lines. Some people criticize M because of its ability to put a very large number of commands on a single line. As we said in previous TYTMs nothing stops a person from splitting lines up. With some commands such as FOR all instructions iterated by it must lie on the same line. This can pose a problem especially when we want to do lots of things once in the loop. We can solve this problem by putting some of the commands in a block underneath the FOR and invoking them with a DO followed by two spaces.
Lets take a closer look at the following fragment of SCRN005
.
.
.
ÞFOR
COL="BLACK","RED","GREEN","YELLOW","BLUE","MAGENTA","CYAN","WHITE"
D SETFGCOL^COLLIB(COL) D SET I=I+1
Þ .D:I#2=1
SETBGCOL^COLLIB("WHITE")
Þ .D:I#2=0
SETBGCOL^COLLIB("BLACK")
Þ .W /EL(2),I,". ",COL,!
Þ .Q
.
.
.
When the DO<two spaces> is encountered is processed commands on the lines below it. It knows which lines it must pass through because these lines commence with a period (.). It will continue down the block until it either encounters a QUIT or until it gets the the first line not having a period. Once the block ends, the instruction following the DO is processed (in our example, SET I=I+1).
Try rearranging the SCRN005 so that all instructions other than the FOR will be blocked.
Alter the programs in MWM002 to operate in this manner.
Note to M Companies The Source has been set up so that you can explain your wares better. As the name states, this section must help explain what a product does, and, more importantly, how it does what it does. A tutorial (that should contain worked examples) is the type of information our readers expect. If you are interested in booking this section for the March 5, 1997 issue of MWM, drop us a note as soon as possible. Address your e-mail to mwm@mcenter.com with the subject set to MWM The Source. In your e-mail tell us, in a paragraph or two, a little bit about the product (only one) you are proposing together with a rough idea of what you have in mind for your presentation. MWM will normally offer The Source on a first come, first served basis. |
Our last program in this issue demonstrates two new mnemonics. /MSMSC saves the cursor position, while /MSMRC, restores the same. We will not comment any further and this program is quite simple to follow.
SCRN006Þ
;Program demonstrates some of the Mnemonic namespaces - Feb 1997
- ACB
Þ;M Web Magazine @
http://geocities.datacellar.net/SiliconValley/7041/mwm.html
Þ;You are using this program at your own risk
Þ;
ÞW #
10ÞW
/MSMSC
ÞW
"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
ÞH 5
ÞW /MSMRC
ÞW
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
ÞW /MSMSC
20ÞR
/CUP(20,1),"Another Line? (Y/N): ",ANS#1
ÞQ:ANS="N"!(ANS="n")
ÞI ANS'="Y"&(ANS'="y")
W /BEL G 20
ÞW /MSMRC
ÞG 10
Goodbye
Our reviewers have noted that probably this is the most dense article to every appear in TYTM. We have in fact split this issue into parts to make it easier for you to assimilate the different parts. This is not the end of this fascinating and important topic. Next issue we will be delving on this subject even more. In the meantime, send us your problems and well try to give you an answer in the shortest possible time.
Format it
The table below gives a list of the X3.64-1979 Mnemonic Namespace that comes with MSM. This is not the only one, but we are sure that it will provide all the controls you will need.
|