Cable Column

Author(s): Bill Cable
Publication: Update Magazine
Pub Details: Update October 1992
Date: October 1992
Pages: 23-24

ARCHIVE SERIES PART 10 : IF and ALL and MODE COMMANDS

The next two ARCHIVE commands to be introduced are the IF command and ALL command. Both have beginning and ending statements like the WHILE command discussed last time. We will also mention the MODE command to illustrate useful control of the display screen.

We are working from the ARCHIVE command prompt (>), still hoping to attract more neophytes before we tackle formal ARCHIVE programming. We are doing complex tasks by putting more than one command on a “line”. The colon (:) is used to separate commands as we type them in and then <ENTER> is pressed to execute the “line”. I call this Command Prompt Programming. They are simpler than real programs created with the ARCHIVE program editor that can be saved and loaded but powerful enough to be very useful

PREPARATION

In the description below I will “italic” what you should type in to participate in the examples. The “line” to type in may span several column lines. You must type everything as one line up to the <ENTER> and then press the ENTER key. On your screen it may or may not wrap around to span more than one screen line. Take special care to spot where spaces are. Putting in extra spaces usually will not cause problems but leaving a space out will most likely result in an error. If you do get an error after pressing <ENTER>, press the <F5> key to recall the line and look it over carefully for mistakes. Correct mistakes with the normal QL editing keys.

We will use the LOOK command on the GAZET_DBF database supplied with ARCHIVE. Make sure you use a copy and not your original. Start ARCHIVE (DATABASE) and access GAZET as shown below :

look "{}gazet"<ENTER>     {}=device,mdv1_,flp1_, etc
print count()<ENTER>      see how many countries
display<ENTER>            display current record
order pop;a<ENTER>        order by population

MODE COMMAND

The MODE command allows for setting the screen display with specific partitions or as one large shared area. Initially you see the screen with 3 areas : Control, Display, and Work. Pressing the <F2> key will toggle the Control area in and out. The Control area is that area at the top which names the commands and provides information. The Display area is in the middle and is where output is displayed. The Work area is where the ARCHIVE prompt (>) waits for your input. By using the MODE command we can join the Display and Work areas or have them separate. Try the following and note where the ARCHIVE prompt (>) is :

<F2>                No Control area
<F2>                Control area
MODE 0<enter>       Display & Work joined
<F2>                No Control area
<F2>                Control area
MODE 1<enter>       Display and Work apart
<F2>                No Control area

You see that there are lots of possible ways to set up the screen. We have left ourselves in MODE 1 so the Work and Display areas are separate and the Control area is removed. When running ARCHIVE programs we would use MODE 0 since the Work area is not used so we don’t need to reserve space for it. For command Prompt Programs as we will be using now, it is a little less confusing if we keep the Work and Display areas separate so we use MODE 1. You can always get the Control area back by pressing the <F2> key to the ARCHIVE prompt. This is all you will every have to know about the mode command unless you use a TV for a monitor.

IF COMMAND

The IF command is a 2-part or optionally 3-part command. It has a starting statement of the form :

if {numerical expression}

Which can be followed by ARCHIVE commands. Then optionally there can be a statement of the form:

else

Which can be followed by more ARCHIVE commands. Then there must be an ending statement of the form :

endif

So it is a conditional branching statement allowing us to control the flow of statement execution based on the value of a numerical expression. Statements after the “if” and before an “else” or the “endif” will only be executed if the numerical expression directly following the “if” is true (nonzero). If there is an “else” as part of an IF command, the statements between the “else” and “endif” will only be executed if the numerical expression is false (zero).

Here is a simple command prompt program which shows how the IF command works :

let x=0:if x:print "True":else:print "False":endif<ENTER>

Note that the numerical statement after the “if” is simply the variable x. Because x was set to zero (false) the statement print “False” after the else statement is executed and we see the word False printed on our upper screen. Now press the <F5> key to recall the statement. Alter “x=0” to be “x=1” using the editing keys. It should now appear as follows:

let x=1:if x:print "True":else:print "False":endif

Now press <ENTER>. Since the variable x is now true (nonzero), the print “True” after the if x is executed and we see the word True printed on the upper screen.

Now for a more complex example using the WHILE command introduced last time. Remember we have accessed the GAZET database. We will use the eof() function which remains true unless we try to move past the last record of the database with the NEXT command. We will print out all sparsely populated countries (few people per area) using the ratio of the pop field with the area field in our numerical expression to branch on.

first:while not eof():if pop/area<.01:print country$;tab 20;pop;tab 30;area;tab 40;pop/area:endif:next:endwhile<ENTER>

Notice that the order of the countries printed is by increasing population which makes sense because we have ordered by the pop field and started at the FIRST record and used the NEXT statement to move sequentially through them. Press <F5> to recall the line and we will alter it slightly to move from last to first. It is a little more complicated because there is no function like eof() to test if we try to move past the first record. In this example recnum() is a function which returns the record number of the current record (they go from count()-1 to 0). We use it to sense when we reach the first record (recnum()=0) and can stop our WHILE loop.

last:let x=recnum():while x:let x=recnum():if pop/area<.01:print country$;tab 20;pop;tab 30;area;tab 40;pop/area:endif:back:endwhile<ENTER>

Now notice the countries are printed out in reverse order of last time because we started at the last and worked our way towards the front.

You can use logical “and” and “or” and “not” within the numerical expression of the IF command. So lets look at only large sparsely populated countries. Press <F5> to recall the last command prompt program and alter to be like the previous one and add a little more in the numerical expression so it appears as:

first:while not eof():if pop/area<.01 and area>1000:print country$;tab 20;pop;tab 30;area;tab 40;po p/area:endif:next:endwhile

Now press <ENTER>. Notice that all the small area countries have been eliminated by the and area>1000 added after the pop/area>.01. It never hurts to use parentheses in the numerical expression to add clarity so :

pop/area<.01 and area>1000

can more clearly be read as :

(pop/area<.01) and (area>1000)

You can also nest the IF statements within one another but you must always have an “endif” for each “if”. Those familiar with SuperBASIC know that you can be sloppy and not match each “if” with an “endif”. SuperBASIC will implicitly assume you meant to put an “endif” at the end of the line. ARCHIVE requires you to be conscientious and if you leave out “endif”s or “endwhile”s or “endall”s you will get an error. Press <F5> to recall the last command prompt program and alter it so it has the following nested if so our listing is organized by continent :

order continent$;a:let c$="":first:while not eof():if pop/area<.01 :if c$<>continent$:let c$=continent$:print:print c$:endif:print tab 2;country$;tab 20;pop;tab 30;area;tab 40;pop/area:endif:next:endwhile<ENTER>

ALL COMMAND

The ALL command is a 2-part looping command. It has a starting statement of the form :

all

Later there must be an ending statement of the form :

endall

Statements between the “all” and “endall” will be executed once for all records currently selected. If an error occurs within the loop execution will be stopped. Pressing the escape key, <ESC>, will also halt the loop but if any other key is pressed before <ESC> it will block the <ESC> from halting the loop. This means that if you start an “all” loop you may not be able get control of the program again until it has finished. On databases with lots of records it can take considerable time for an “all” loop to finish.

So an ALL loop works much like the loops done above using the WHILE and NEXT commands to step through the records. The coding is even simpler:

order pop;a<ENTER>
all:if pop/area<.01:print country$;tab 20;pop;tab 30;area;tab 40;pop/area:endif:endall<ENTER>

Although the database is ordered by the pop field, notice that wasn’t the order they came out in. Unlike the looping using NEXT or BACK with a WHILE command that works sequentially through a database off the currently defined order, the ALL command has no user predictable order as it works its way through all the records. Because of this, never ALTER, INSERT, DELETE, OR APPEND within an ALL loop, use the WHILE loop instead. The ALL loop is faster and easier to use than the WHILE loop. That makes it handy for quickly looking through a database:

all:print country$:endall<ENTER>

Combining the ALL command with the IF command can make fast inspections possible:

all:if continent$="AFRICA" or continent$="ASIA":print country$:endif:endall<ENTER>

all:if not (continent$="AFRICA"):print country$:endif:endall<ENTER>
close<ENTER>          Always close files when done

Next time we will look at the PRINT and LPRINT commands and at useful ARCHIVE functions.

Until Next time Happy Archiving!