Cable Column

See all articles from Update April 1992
Author(s): Bill Cable
Publication: Update Magazine
Pub Details: Update April 1992
Date: April 1992
Pages: 21-22

ARCHIVE SERIES PART 8 : ORDER and SELECT COMMANDS

The ORDER Command was introduced in the last issue of UPDATE. Now we will complete our discussion of it and move on to the SELECT Command. The ability to order on the fields of a database and quickly locate needed information using that order is the most powerful feature of a database system. It repays us for all the effort it takes to organize our information in the record/field format. Although we have covered the general principles of order we need to give a few more examples and also illustrate the difference between ordering a “looked” database versus an “opened” database. Next we will explore the SELECT Command which allows us to make temporary subgroups of our database by specifying certain criteria about the fields.

Before getting into that let’s celebrate the fact that this series is marking the end of its second year. During that time, UPDATE has provided a reliable soap box for me to stand on preaching the virtues of this excellent database system. I even hear from time to time that the column is being read and used. I have tried to make the series easy for beginners to join at any time but soon we will start building on what we have covered and begin writing ARCHIVE programs. The best kind of programs for this column to cover are ones that will be useful to its readers. Please feel free to request programs covering subjects of interest to you. For those wanting a complete collection of the series, back issues of UPDATE are still available and here is an issue index :

  UPDATE ISSUE INDEX FOR THE ARCHIVE SERIES
Jul 90  Part 1 : Database Concepts  14-17
Oct 90  Part 2 : Archive Data Structure  37-40
Jan 91  Part 3 : First Commands  23-25
Apr 91  Part 4 : Create, Open, Insert Commands  25-26
Jul 91  Part 5 : Alter and Delete Commands  33-34
Oct 91  Part 6 : Find,Search,Continue Commands  36-37
Jan 92  Part 7 : Order, Locate, Reset Commands  33-34
Apr 92  Part 8 : Order and Select Commands

Altogether it comprises 20 pages of practical ARCHIVE tips gained through extensive experience. It should prove invaluable to anyone wanting to use ARCHIVE. Maybe you don’t have time to seriously tackle ARCHIVE right now but if you have the issues the information will be there when you need it. That is why its important to keep supporting UPDATE, IQLR and User Group publications. Information gets collected in a nice format so we have it when we need it. I would also like to encourage others to write more for these publications. I have never read a QL related article that didn’t give me some useful information and insight. Never feel that your experiences are too insignificant to share. How hard is it write an article? Load QUILL and put down your thoughts. Save it to a disk or microdrive. Send it to the intended publication and you will have made one editor’s day.

PREPARATION

In the description below I will “italic” what you should enter to participate in the examples. We will use the OPEN command on the GAZET_DBF database supplied with ARCHIVE and permanently change it so make sure you use a copy and not your master. Start ARCHIVE (DATABASE) and go through the following collection of commands as a refresher. <ENTER> means press the ENTER key :

look "{}gazet"<ENTER>         {}=device,mdv1_,flp1_, etc
print count()<ENTER>          see how many countries
print recnum()<ENTER>         current record number
display<ENTER>                display current record
find "nassau" <ENTER>         find something
print recnum()<ENTER>         current record number
next<ENTER>                   move forward one
display<ENTER>                see current record again
search pop>=100<ENTER>        field meeting condition
continue<ENTER>               next one
order pop;a<ENTER>            order by population
locate 100<ENTER>             locate on population
let a=50<ENTER>
locate a<ENTER>               use variable with locate

ORDER COMMAND (continued)

In Part 7 we covered the basics of the ORDER Command. We saw that we can quickly define one order for an ARCHIVE database using 1,2,3, or 4 fields. It is usually not useful to order on more than 2 fields. If we had an address database with fields for last name and first name it is easy to see that it would be handy to have order by last name and then first name so we could separate people with the same last name but different first names. Having only one order available at any given time is a handicap. PSION’s PC version of ARCHIVE can keep 4 sets of order active at the same time and is the one big advantage of PC ARCHIVE over QL ARCHIVE. For a book catalog database it would be nice to be able to look up by title or author at a moments notice. With QL ARCHIVE we can have it ordered on one or the other. If we want a different order we have to execute the ORDER Command and wait for a short time for the new order to be completed. Let us order GAZET by continent and population and play with that :

order continent$;a,pop;d<ENTER>
locate "EUROPE",1000<ENTER>   most populous first
next<ENTER>                   population decreases
<F5><ENTER>
locate "ASIA",100<ENTER>      first ASIA under 100
locate "E"<ENTER>             first EUROPE
locate "EUROPE"<ENTER>        just beyond EUROPE

These are tricky because there are two fields in the order and the second field, pop, is ordered in descending order. Try the same order command only replace the “d” after pop by an “a” and try the same commands. Leaving out the 2nd field in locate defaults to 0 or null.

One property of an ARCHIVE database is that whatever order existed under the last OPEN access will be the one that is in effect the next time the database is “looked” or “opened”. If you only ordered the database when it was accessed by “look” then no order is in effect when it is first accessed. Try the following :

close<ENTER>                  close GAZET_DBF
open "{}gazet"<ENTER>         device (mdv1_,flp1_,etc)
order country$;a<ENTER>       make a permanent order
close<ENTER>
look "{}gazet"<ENTER>
locate "D"<ENTER>             locates by country
order capital$;a<ENTER>       make new order
locate "D"<ENTER>             now locates by capital
close<ENTER>
look "{}gazet"<ENTER>
locate "D" <ENTER>            order by country still on
reset<ENTER>                  remove all order
locate "D"                    error 8 since no order
close<ENTER>

What I find useful for each database is to define the primary (permanet) order (most commonly needed order) with the database accessed with OPEN. Then I define other orders and select or reset only while looking at the database so I never remove the primary order. To get my primary order I only need close and look or open again (usually faster than reordering a large database). Any order in effect is maintained while adding, altering, or deleting records. Only the RESET or ORDER commands will alter the current order. Remember that there is no way to tell what the current order is. You must keep track of that yourself.

SELECT COMMAND (FILTERING)

The SELECT command allows one to select a subgroup from the currently selected records. It has the form :

select (field condition1) and/or (field condition2) ....etc.

Each field condition evaluates to a numeric expression usually a logical statement (0 is false, nonzero true). When a database is first accessed all records are selected. Each time a select command is executed some records can be filtered out depending on the select condition. Any order in effect when a selection is made remains in place after the selection. If a reset is done to make all records active again then all order is lost. If the database is being “looked” at then any permanent order made when it was “opened” will still remain when it is re-accessed again.

look "{}gazet"<ENTER>         {}=device mdv1_,flp1_, etc
print count()<ENTER>          all records active
display<ENTER>                display current record
locate "D"<ENTER>             country order active
select pop>20<ENTER>          countries with population>20
print count()<ENTER>          see how many now active
display<ENTER>
locate "D"<ENTER>             no "D" so "EGYPT"
select continent$="ASIA"<ENTER>
print count()<ENTER>
display<>ENTER>
next<ENTER>
<F5><ENTER>
(repeat until end is reached)

We ended up with all Asian countries with a population of greater than 20 million still ordered by country. We could have done the selection in one statement :

reset:print count()<ENTER>
select pop>20 and continent$="ASIA"<ENTER>
print count()<ENTER>
display<ENTER>

We could use an or statement to get quite another subgroup

reset<ENTER>
select pop>20 or continent$="ASIA"<ENTER>
print count()<ENTER>

Now we have all countries with a population greater than 20 million or in Asia. Using the INSTR() function allows for interesting options (see pg 51 ARCHIVE manual).

reset<ENTER>
select instr(languages$,"ENGLISH")<ENTER>
print count()<ENTER>
display<>ENTER>

Now we have selected all countries which have English as a major language. A quick way to see all selected records uses the ALL statement which will be described later :

all:sprint:endall<ENTER>

You noticed that “ENGLISH” appeared somewhere in the Languages$ field. The INSTR() function allows us to make use of that even though other words may also appear in the field. We could see the same records although not select them using the SEARCH Command which uses syntax similar to the SELECT Command :

reset<ENTER>
search instr(languages$,"ENGLISH")<ENTER>
while found():sprint:continue:endwhile<ENTER>

The WHILE Command will be covered later but is illustrated to show ARCHIVE has many tools available.

The SELECT Command is a powerful tool that allows the user to temporarily work with a subgroup of the database meeting specified conditions. Using the EXPORT Command (described later) the subgroup can even be exported to be its own database if desired.

Next time we look at the UPDATE Command and WHILE Command and write some single line programs. Until then Happy Archiving!