• Keine Ergebnisse gefunden

12. DECLARED SCALAR TYPES

13.3 ORDER OF ELEMENTS

The elements of an array are stored in a specific order. The order is different for CP/M-80 systems than for all other version of TURBO Pascal. The following description applies to all versions of TURBO Pascal except CP/M-80:

If the array is one-dimensional-that is, if it has only one index-then the elements are stored in ascending order. For example, the array List (defined as array [0 .. 9] of Integer) stores its elements in the order List[O] , List[1], List[2] , and so on ... basically what you would expect. But what about multi-dimensional arrays? The array Stars is defined as

var

Stars : array[XRange,YRange] of O .. StarMax;

where SRange

=

1 ... 8. So, the question is, are the elements in Stars stored as Stars[1,1], Stars [2, 1], Stars [3, 1], etc., or are they stored as Stars[1,1], Stars[1,2], Stars[1,3], and so on? Pascal itself gives you the answer to that question. Remember that your definition above is just shorthand for

array[XRange] of array[YRange] of o .. Starmax;

In other words, Stars' first index doesn't select an element, it selects an array[YRange] of O .. Starmax. The second index selects an element within that array, and those elements are stored sequentially, just as in List. So Stars[1, 1] says to pick the first element of the first array; Stars[1,2], the second element of the first array; and so on. So the elements in Stars are stored in the order

Stars[l,l]

Stars[1,2]

Stars[1,3]

Stars[1,4]

Stars[1,6]

Stars[1,6]

Stars[1,7]

Stars[1,8]

Stars[ 1, 9]

Stars [ 1,10]

Stars[2,1]

Stars[2,2]

Stars[8,9]

Stars [ 8,1 0]

All you need to do is remember that the index furthest to the right-the last index-changes the fastest. If you have the array var

BigOne : array[0 .. 3,0 .. 4,0 .. 6,0 .. 2] of Byte;

then you can quickly work out that the elements are stored as BigOne[ 0,0,0,0]

BigOne[ 0,0,0,1]

BigOne[O,O,O,2]

BigOne[O,O,l,OJ

BigOne[ 3,4,6,1 ] BigOne[ 3,4,6,2]

The above description generally applies to CP/M-80 systems, with the following important difference:

The elements of the array are stored in descending rather than ascending order. When you address the array, the first element is located just as in other versions; however, additional elements will be found in descending memory locations rather than in ascending memory locations

14. STRINGS

-String-When Niklaus Wirth designed Pascal, he did so in a punched-card/mag tape/mainframe environment, where fixed-length data were the rule. At least, that's probably the reason he was satisfied to store a character string as a array[1 .. n] of Char. At any rate, Standard Pascal does not (currently) have a predefined data type for strings. String constants such as

cODSt FUeName LifeName

= 'B:STABS.DAT';

= 'WHATEVER YOU WANT';

are considered to be array[1 .. n] of Char, where n equals the number of characters in the string.

The early mainframe (that is, very large) computers were batch-oriented systems. "Jobs" were "submitted" using large decks of punched cards or reels of magnetic tapes, and the results came

out on a high-speed line printer. But the arrival of interactive operating systems ("timesharing") and the CRT terminal started a new approach to computer use. When the minicomputer showed up, so did the first code designed to interactively manipulate text: word processing programs. The explosive growth of the microcomputer market over the last 10 years has been matched by an equal growth in word processors and the number of people using them. For all its reputation as a number-crunching machine, the computer is used most often to move words, not val ues.

The basic concept behind text manipulation is that of a string. A string is simply a sequential list of characters of some length. For our purposes, the characters belong to the ASCII character set (the back of the TURBO Pascal Reference Manual gives you the list). The string can contain letters, digits, and punctuation. It can even have non-printing (control) or special characters. You can pick out parts of the string, add to it, take away from it, combine it with other strings, print it out, read it in-in short, you can manipulate it.

TURBO Pascal allows you to declare a variable to be of type string, followed by a length specification. Forexample, you could define the following:

var MyName Token BigString

striDg[ 80];

striDg[15];

striDg[ 255] j

Note that you must specify a length for each variable. This defines the maximum number of characters that each string can hold. The variable MyName could hold up to 80 characters.

Token could only hold up to 15 characters, so that the statement Token:= 'this is too long a string for token'j

would only store the first 15 characters ('this is too Ion') into Token. The last variable, BigString, represents the maximum length possible for a string-255 characters.

The data type string[n] can be thought of as an array[O .. n] of Char. For example, you can reference individual characters in Token using the notation Token[1], Token[2], and so on. The first location, Token[O], contains the current length of Token. If you execute the statement

Token:= 'this string';

then Token[O] contains the value 11, since there are 11 characters in'this string'. However, you could not do something like this:

program Example;

var

Token : striDg[16];

Len : Integer;

begin

Token.: = 'this string';

Len:= Token[O];

WriteLn('The length of token is ',Len) end.

Why not? Because Token[O] is of type Char, and you can't assign a character to an integer. You could, however, substitute the statement

Len:= Ord(Token[O]);

which would return the ordinal (numeric) value of Token[O], which happens to be 11.

Using the array notation, you can play with any individual character of a string. As mentioned above, each element of a string is a variable of type Char, and you can treat it as such. For example, you might want a procedure to convert all letters in a string to uppercase ('A' .. 'Z'):

procedure LowToUp( var Str : striDg[266]);

{

purpose converts characters in Str to upper case last update 27 Oct 86

var

Indx,Len : Integer;

begin

Len:= Length(Str); {more on this later}

for Indx := 1 to Len do

Str[Indx] := UpCase(Str[Indx]) {built-in TURBO func}

end; { of proc LowToUp }

A caveat (warning) is in order. You should avoid trying to mess with any elements beyond the current length of the string.

TURBO Pascal won't give you any sort of error, but you need to be aware that you've just changed a portion of the string that won't print out unless you change the length as well. (Note that if you use the {$R+} compiler directive, and error message will display.)

You've already seen that you can assign string constants to string variables (Token := 'this string'). You can also read and write strings through textfiles (which will be discussed in a later section). Most notably, you can read and write strings through the predefined textfiles, Input and Output. For example, this program allows you to type in a line (up to 80 characters!) and then writes it back out to the screen. It continues to do this until you type, "1 quit!":

program Echo;

var

Line : striDg[ 80];

begin

WriteLn('Entering echo mode-type"I quit!" to exit');

repeat

ReadLn(Line );

WriteLn(Line) untU Line = 'I quit!' end. { of program Echo}

Now, let's make the following modifications to your program (don't worry about the file stuff; yes, you'll learn about that later, too):

program MicroWord;

var

Line : striDg[ 80];

OutFile : Text;

begin

Write('Enter file name: '); ReadLn(Line);

Assign( OutFile,Line); Rewrite( OutFile);

WriteLn('Enter1n.g insert mode-type"I quit!" to exit');

repeat

ReadLn(Line );

if Line <

>

'I quit!'

then WriteLn(OutFile,Line) untU Line = 'I quit!';

Flush( OutFile); Close( OutFile) end. { of program MicroWord}

Voila! You've just written a word processor. However complex or sophisticated they may seem, all word processing programs eventually boil down to the program above. Start with this program, add modifications, and eventually you'll have your own text editor.