Contents of the ARMFRED2.TXT file
Welcome back to the Armchair! I hope you are comfortably seated, with
your computer sitting nearby, and your mind fresh and ready for new
ideas. In this chapter of "Armchair FRED" we will continue our
discussion of storage techniques, and then move on to using special
functions to manipulate numbers and strings. And so, without further
ado. feet up, TechNotes open, we're off.
More about Storage
Since we are going to talk about storage, it is a good idea to discuss
a function that actually does most of the storing for us. In my
previous examples, I have freely used expressions like "Temp := 10"
and have not explained the use of the ":=" symbol. It is called the
assignment operator, and has exactly the same result as @set(Temp,10),
meaning that the value 10 is placed in the variable Temp. Temp can be
a local variable, a frame, or a cell. Please note that ":=" is not
the same as "=". The equals sign alone is used for comparing two
values and does not change either value. Equivalency and other
comparisons will be discussed in a later Armchair session.
Before moving on to another facet of programming, I want to describe a
useful feature of storage in Framework. Suppose that you have a word
frame called Letter, which contains about four pages of text. You
decide that the program you are writing needs to work with that frame,
maybe because your program will break up the word frame into several
page-sized frames. We do not need to worry about the actual program
right now, but let us consider the storage options.
It would be very convenient to be able to treat the word frame as a
large string variable and apply FRED's string functions to it. This
is quite easy to do. For example, you want to know what the 200th
character is in the Letter frame. The function @mid returns the value
of a particular string portion, based on some coordinates you give
it. The following expression uses @mid to put the 200th character
from Letter into another frame called Test, which is being used as a
variable in our hypothetical program.
Test := @mid( Letter, 200, 1 )
Note the three items within the parentheses that are separated by
commas. These are called the "arguments" of a function, and in the
case of @mid, they represent the string to work with, the character to
start with, and the number of characters to return, respectively. The
syntax of this expression is the same whether you use a frame, as with
Letter, or a cell, such as Sheet.C5, or a local variable. Carrying
this a little further, we could actually assign a word frame of up to
64,000 characters to a local variable like this:
@local( temp ), ; Initialize local variable.
temp := Letter, ; Assign word frame to temp.
Test := @mid( temp, 200, 1 ) ; Test frame gets 200th char.
This produces the same results as before, but you can see that no
special functions are required to assign the word frame to any other
variable. That is enough on storage for the time being. Our next
subject concerns the real workers in FRED: the functions that work
with strings and numbers.
Working with Value Functions
If someone asked me to divide FRED functions into general groups by
function, I would create two such groups. The first group could be
called "control" functions, and would include the functions used to
perform looping, testing, region walking, and input/output. These
functions provide something like a system of traffic lights for your
program, making the decisions on where to go and when to stop. There
are many differences among these functions, so they will be covered
separately later on.
The second group I'll call the "value" functions. It includes the
functions that operate on string and numeric values, such as
conversions and calculations. Please note that when I use the word
"value" I am referring to numeric and string values in general.
To begin our discussion of value functions, create a spreadsheet that
looks like the one in Figure 1.
Now is as good a time as any to discuss regions, which play a big role
in FRED programming. If you think about it, the things you are most
likely to do with Framework will involve using groups of cells in
spreadsheets or databases. A region is a particular group of cells
that are to be used in some FRED calculation. In the sample
spreadsheet above, we would use the region B2:B4 to refer to the three
numbers in the Sales column. As an illustration, if you were to write
down a range of page numbers that you wanted to remember, you would
probably write "pp. 6-17," meaning "pages six through seventeen."
The concept is the same with a region in FRED. B2:B4 means "cells B2
through B4," and is three vertical cells in column B. A range must
contain only adjacent cells, though they may be in a row or a column
or even a rectangular block. To specify a block of cells, use two
diagonally opposite corner cells. We could specify all the numbers in
the Sales Totals spreadsheet with the range B2:D4, using the upper
left and lower right cells of the block. Any reference to this
spreadsheet from a different frame would have to use the formal region
reference that includes the frame name, such as [Sales
Let's put some functions in this spreadsheet to demonstrate how they
would be used. One of the most common is @sum, and it will work just
great for us when we need to calculate the totals for each column. The
@sum function typically has one argument: a region reference. In our
case, the region is a column. To calculate column totals for the
above spreadsheet, first set up a row title by moving the cursor to
cell A6. Second, type Total and press Return. Third, move to cell B6,
press F2 (Edit Formula), and type @sum(B2:B4) followed by Return. The
total will appear in the cell almost immediately. Press F8 (Copy),
rightarrow, F6 (Extend Select), rightarrow, and Return twice to see
the other columns totaled. The formula from B6 is copied into C6 and
D6, except that the region reference has been automatically changed to
be appropriate for each of the new totals. If you have not run into
that before, it is called relative referencing, and is a part of the
cursor-pointing and COPY (F8) commands. For our purposes, I just want
you to notice the use of the @sum function, because many of the FRED
value functions are used in the same way. Go edit the formula in B6,
changing the @sum to one of the other statistical functions like @avg,
@count, or @max, and get the feel of how they work.
It is important that you understand how to use region references
within a function, which is why I chose to discuss them first. Many of
FRED's value functions are simple and only require that you read the
manuals to discover the proper syntax. Using certain of these
functions may require knowledge of advanced mathematics which is, of
course, beyond the scope of the manuals and of this article; but the
average user will have no need for this. My goal is to give you an
understanding of how to use functions in general, and how to use your
own creativity to find the solutions to any puzzles you may
encounter. Because they are much like standard spreadsheet functions
and somewhat self-explanatory, I will let you explore most of the
numeric functions (like @abs, @exp, @round, and @int) on your own and
we will move on to string functions.
A spreadsheet was convenient for discussing functions like @sum
because spreadsheets are designed to manipulate numbers. Databases,
however, usually handle character data (strings) and we will use one
now to demonstrate string functions. Figure 2 shows the structure of
our example database.
I am going to be mean here, and not tell you how to create that
database frame. If you have completed the Using Framework III manual,
you will have no problem doing it. Just set up the database with the
structure and data shown in Figure 2. What we want to do here is to
create a field that can be used to print mailing labels. By placing
the correct formula in the Label field, we can combine all the other
fields into a label format and store the result in the Label field.
Get into the database and place the cursor on the Label field. Now
press F2 (Formula Edit) and F9 (Zoom) to edit the Label field
formula. Enter this formula:
Label := Title & " " &
First & " " &
Last & " " & "
" & Address & "
" & City & ", " &
State & " " & Zip
Press Esc when you are done. To see the result of this formula, move
the cursor down to the one record you had entered and press F5
(Recalc) on the Label field. You will see some of the information
appear, but by now you probably realize that only one line of data can
be seen in a database field unless you zoom on it. So press the
spacebar and then F9 (Zoom) to examine this field. What do you see?
A three-line address that would fit on most labels, right? Let's
examine the formula that did it.
First, we have the field name, Label, and an assignment operator.
This indicates that a value is being assigned to that field. Note that
there are no commas in the formula, and that it is really considered
to be one function. The neatly ordered format, with fields and
ampersands lined up under each other, demonstrates that FRED ignores
spaces and Returns. You can format a program to be easily readable to
you, and not worry about affecting the program's operation.
Next, we have the expression that creates our label string. You can
see each of the field names listed there, as well as a bunch of
ampersands (&). The ampersand character is called the "string
concatenator" which means that it can join two strings together to
make a larger one. As an example, "abc" & "def" would produce the
string "abcdef". So our formula starts with a person's Title, adds a
space, adds the first name and a space, then adds the last name, a
space and @chr(13). Here we meet our first string function. @chr will
produce a one-character string from the ASCII value in the
parentheses. @chr(13) puts a Return character into our label because
13 is the ASCII code for a Return. The rest of the Label formula is
self-explanatory. Especially note the use of the Returns and the
comma inserted after City.
There is just enough time for one more string function example.
Suppose you wanted to print the first initial on the labels instead of
the full first name. What function might help us do it? The answer
is @mid. This function is used when you want to select a portion of a
string. It takes three arguments: a string, a starting position, and
a length. Here is what our modified label field formula looks like:
Label := Title & " " &
@mid(First, 1, 1) & ". " &
Last & " " & "
" & Address & "
" & City & ", " &
State & " " & Zip
@mid takes the first character of First and follows it with a period
and a space. Pretty simple, isn't it? Think about the strings in your
programs as collections of characters, and you can manipulate the
characters like building blocks, using the string functions and the
ampersand to manipulate your string data into any form you like.
That's about it for the introduction to value functions. The only way
to become really proficient with all of them is to try each one
yourself. Of course, I'm hoping that our excursions from the armchair
will stimulate your curiosity and tempt you to experiment. Next time,
in Part Three, I will be discussing looping and testing, two very
important concepts for controlling your programs. Until then, may FRED
be with you.