It is currently Sun Jun 24, 2018 3:35 pm


All times are UTC - 8 hours [ DST ]




Post new topic Reply to topic  [ 1 post ] 
Author Message
 Post subject: Unit 2 - Lesson 10: Expressions – Operators
PostPosted: Sun Jun 26, 2011 3:16 pm 
User avatar

Joined: Wed Nov 17, 2010 8:37 am
Posts: 136
Real Name: Terry L. Wiechmann
Began Programming in MUMPS: 0- 0-1971
Up until this point we have studied the concept of a string which can manifest itself as a string, number or truth value based on the context it is being operated upon. We then studied symbols (arrays), special variables and functions, abstract value producing entities that produced one of the string types. Although each of these concepts is formally an expression in its own right, they are rather restrictive. The goal of this lesson is to introduce operators that operate upon strings and value producing entities to create even more powerful expressions.

Operators are grouped as follows:
  • Arithmetic - When a string is operated upon by an arithmetic operator, the context demands that a string be treated as a number. If not already in numeric format, it will be converted for the operation.
  • String - The string operator operates on the whole string. Of course, any of the string types are strings.
  • Logical - When a string is operated upon by a logical operator, the context demands that it be treated as a truth value. If not already a number (zero is false and non-zero is true), it will be converted for the operation.
  • Relational – These operators come in two flavors – String or Arithmetic. Depending on the operator used, the context will either require a string or number respectively.
  • Pattern Match - Strings are operated upon by the pattern matching operator. We will cover this in detail.
Building Expressions using Operators

This is a simple example of an expression:

A+2/B

This is another:

+$H ;Produces the linear date number from the Horolog special variable.

When constructing any type of expression in a computer language, you need to know about precedence. That is, the order in which the expression will be evaluated. In some languages, the precedence of expression elements are predefined. You must learn that order and apply it correctly or the resultant value will be wrong.
In MUMPS, the application programmer is responsible for creating the evaluation precedence by enclosing sub-expressions with parenthesis. Although the programmer is responsible for the ordering, make no mistake about it, if the order is wrong, the resultant value will be wrong.

Lets use the following example.

Code:
> Set A=2,B=3
> Write A+2/B
1.333333333333

What if you wanted 2/B evaluated first and then added to the value bound to A?

Code:
> Write A+(2/B)
2.666666666667

Taking another example.
Code:
> Set X=2
> If X=1!X=2
> Write $Test
0

This is not correct because of the left to right evaluation rule. First the contents of X is compared to for equality with 1 giving false (0). Then the false results (0) of that evaluation is or'ed with the contents of X (true because it is non-zero) giving true (1). Then that result is compared for equality to 2 (true because it is non-zero) and the final result is false (0)

Note: Notice that the results of the IF commands logical evaluation is stored in the special variable $Test. This is not true for post conditional evaluations.

This is a very common mistake when constructing logical expressions. In situations like this, you need to order the evaluation process as follows.

Code:
> Set X=2
> If X=1!(X=2)
> Write $Test
1

First the contents of X is compared to for equality with 1 giving false (0). Next, the expression contained in the parenthesis is evaluated. The contents of X is compared to 2 (true because it is non-zero) resulting in true (1). The two resultant logical values are or'ed, that is false (0) or'ed with true (1) which is true (1).

Exercise 1

At the programming prompt, use the appropriate commands to construct Arithmetic, String, Logical and Relational expressions.

Pattern Matching Operator

Pattern Matching is another powerful feature of the MUMPS language. It is an essential feature in a database application. When collecting data via a user interface, it is important to check it for correctness. Pattern Matching is used for this purpose.

At first glance Pattern Matching looks complex. It isn't! Lets break it down and make it understandable.

Constructing a pattern is a matter of breaking the pattern down into sub-patterns. That is:

P=P1P2P3...Pn

where each sub-pattern consists of two parts:

Repeat count followed by a pattern code, string literal or alternation. That is:

Repeat counts are in the form [n] . [m] (where the brackets demote optional) and n and m are integers (0,1,2...). The “.” is an indefinite multiplier. This format can define a specific number or range of numbers. For example:

1 only one
0.2 zero (null) through 2

Note: Remember the number line we used to describe positioning in a string. Any position 0 and less are empty.

.2 same as 0.2
3. three or more (m note specified means to the end of the string).
. zero to length of string being compared.

The repeat count must be followed by either a pattern code, string literal or alternation.

Pattern Code are codes that represent character groupings from the ASCII character set. As stated in the guide, they can be remembered using the mnemonic CAPE NULL For example:

X?1A checks for string one alphabetic (upper and lowercase) long.
X?1.A checks if string is all alphabetic.
X?0.A checks if string is all alphabetic including null.
X?.E checks if string contains any of the ASCII characters including null

A string literal is simply a string. For example:

X?1”John” determines if the entire string is “John”

At this point we have only used one substring to construct the pattern. Lets construct a more powerful pattern by concatenating multiple sub-patterns. For example:

X?.E1C.E checks for a least one control character in the string

When concatenating these sub-patterns you are telling the evaluator that sub-pattern P1 and P2 and P3 must each be true for the whole expression to be true. Sub-patterns are and'ed. That is, they all must be true for the expression to be true.

The example above says scan the string bound to X until you hit a control character. All character up until the control character (if it exists) can be anything including null. Also, any characters at the end of the string can be anything including null.

Up until this point you have a powerful way to check string patterns by and'ing sub-patterns. However, there are situations where you want to check for alternate possibilities within sub-patterns. For example:

Code:
Set X=”Jane Doe”
X?.(1”John”,1”Jane”)1.” “1”Doe”
Write $Test
1

Within the comma separated list sub-patterns are or'ed. The string in X is scanned for John or Jane where the preceding characters can be anything including the null (dot repeater preceding the list) followed by one or more spaces followed by one “Doe”

Note: MUMPSV1 has problems with alternation consequently the above pattern does not work.

Exercise 2
  1. Using the concatenation operator, create a string with a quoted sub-string embedded in it.
  2. Then create the pattern that will check for a quoted sub-string of any length within the string.


Hint: There are a couple ways to create the string by using the $Char function or using the formal definition of how to embed a quote character in a string:

strlit ::= " [ "" ] "

Brackets mean optional.

Make sure you understand pattern matching. You will need it when we do the workshop exercise.

_________________
Terry L. Wiechmann


Top
Offline Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC - 8 hours [ DST ]


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
Theme created StylerBB.net