M Tutorial (part 1/6)
We now have two M tutorials per issue. This column deals with M running on character based interfaces, the other is a tutorial about MSM-Workstation (the native Windows 95 GUI implementation of the language). M is M irrelevant of the platform on which the language is being executed; and therefore the material presented here is important if you are new to M. In the GUI tutorial it is assumed that you are already familiar with the core (syntax) of the language (which is discussed here). The programs in this tutorial can be run on the student version of Micronetics Standard M (MSM/Student). In order to download a copy of this program check out Chris Bonnici's Download M section. Before you leave this page be sure to bookmark it so that you can find your way back with ease. If you would like to know more about the host of products and services Micronetics provide, check out their web site at http://www.micronetics.com. Micronetics is listed in our Thank You page. Drop them an e-mail to let them know that you appreciate the fact that they are helping MWM. Back issues of this page can be viewed by going to the index. If you have any comments or questions we are here to help. Your input could be in the form of general comments giving us the green light, hitting the expand-topic button and even taking us back a step or two. No two people will generate the same solution to a problem. We welcome any code you send in, be it different approaches to what we present here or other code of your choice. Click here to download this issue's routines. In this issue we continue with our discussion of intrinsic functions. You will be please to know that this is the largest M Tutorial to date. $PIECE(<string>,<delimiter>[,<first>,[<last>]]) or $P((<string>,<delimiter>[,<first>,[<last>]]) $PIECE is a function that supports the claims that M has powerful string handling capabilities. Imagine a conference hall with movable partitions. If used in its entirety it can hold 255 seats. The conference hall is a variable we shall call HALL. SET HALL="255" Now let us represent a partition using the tilde (~) symbol. We can split the hall into two parts. One part will contain 100 seats, while the other part will hold 155. In M this would be SET HALL="100~155" Notice that in the above 100~155 is a string (enclosed in quotes). The $PIECE function is used to manipulate and extract string data which is separated by a unique symbol (in our case the ~). The variable SCORE contains within it the numbers 100, 255, 234, 43, 54.3, 63, 97, 40, 123 and 456 delimited by ~. The program below will extract and maniplate the data from this string. INTFN101Þ;Program demonstrates intrinsic
functions - Chris Bonnici - July 1997 The program above uses $P to extract the numbers one at a time into variable NUM. By cross referencing the various PIECE commands with the diagram above you should be able to figure out how PIECE works. You supply the variable you want to extract data from, the delimiter symbol (which could easily be stored inside a variable as we shall see in the program below) and the partition you want. In INTFN101, we extract a partition that doesnt exist S NUM=$P(SCORE,"~",11). No error will be generated although in this case NUM will be empty, represented as a null ("") value. INTFN102Þ;Program demonstrates intrinsic functions - Chris Bonnici - July 1997 INTFN102 has a number of things worth nothing:
Modify the program above to calculate the average of the group. Assume SCORE contains "100~255~234~43~~63~97~~123~". Some partitions are empty (null). Modify your average program so that it will only average those scores that do contain a number. (Hint: $P(SCORE,DELIMIT,I) is not null). Without introducing anything new, we one look at one of the "tricks of the trade" when using $PIECE. There is nothing new to what we already said; it is merely an iteration on how one can nest one delimiter into another. A class of five students during a particular course will sit for three exams. At the end of the year, the lecturers mark sheet will contain: Student 1 :
Mark 1, Mark 2, Mark 3 If the tutor wants to find the 2nd mark of the 1st student, he will first extract the student, following this, he will go for the mark desired. The program below automates this function. INTFN103Þ;Program demonstrates intrinsic functions - Chris Bonnici - Aug 1997 Before we flip the $P coin, there is one point that needs clarifying. We have always used numbers. One can delimit strings as well, for example S NAMES="John~Mary~Peter~Alan~Anne". It is important to re-stress that in M everything is stored as characters because M is an untyped language. Numbers are given their numeric attributes when they are computed upon. The short program provides a coded clarification. INTFN104Þ;Program demonstrates intrinsic functions - Chris Bonnici - Aug 1997 Modify INTFN103 so that the name of the student is displayed when the tutor enters the student number.
|