|
To access the contents, click the chapter and section titles.
Learn Pascal in a Three Days (2nd Ed.)
Chapter 8
|
['a'..'z'] | the set of lowercase letters |
['A'..'Z', 'a'..'z'] | the set of all letters |
[0..9] | the set of digits |
The set is a structured data type that may include unordered elements (or members). You can express a set constant by listing its elements between brackets separated by commas. Unlike arrays, the order of elements in a set is not important. For example, the set [1,3,5,7], which represents the set of odd numbers between one and seven, is the same as the set [1,7,5,3]. This indicates another difference between sets and arrays. In arrays you can access any element by its position in the array, but with sets you cannot access individual elements. You can only test a data item to see if it is a member of the set using the IN operator. If the elements of a set form a continuous subrange, you may use the two periods (..); for example, the set [1,2,3,4,6,8] can be written as [1..4,6,8]. The elements of a set can be of any ordinal type, but all of the elements must be of the same type, which is called the base type.
You can declare a set variable using the keywords SET OF, as in this example where a set of the base type CHAR is declared:
VAR LowerCase:SET OF CHAR;
After this declaration you can assign the variable LowerCase a set constant of the base type CHAR, for example:
LowerCase:= ['a'..'z'];
You may then test a variable of the type CHAR for membership in this set using an expression like:
IF Character IN LowerCase THEN...
Note that if you use the expression IN [ 'a'..'z'] there is no need for declarations.
As with other structured types, it is preferable to declare sets in the TYPE section; you can then use this type in the VAR section to declare variables. The declaration takes the form:
type-identifier = SET OF base-type;
Here is an example:
TYPE Days = (Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday); Languages = (C,CPP,Pascal,Fortran,Basic,Cobol,Assembly); Digits = SET OF 0..9; Lowercase = SET OF 'a'..'z'; Uppercase = SET OF 'A'..'Z'; DaySet = SET OF Days; LanguageSet = SET OF Languages; CharacterSet = SET OF CHAR; VAR WholeWeek, WorkingDays, WeekEnd :DaySet; OddNum, EvenNum, Numbers :Digits; Small :Lowercase; Capital :Uppercase; ProgCodes, HLL, LLL, MLL :LanguageSet; Alphabet :CharacterSet;
In these declarations, variables such as Weekend, WorkingDays, and WholeWeek are all sets of the base type Days. Any of these set variables may be assigned one or more elements of the enumeration Days, such as:
WeekEnd:= [Saturday,Sunday]; WorkingDays:= [Monday..Friday]; WholeWeek:= [Monday..Sunday];
The last statement is equivalent to the statement:
WholeWeek:= [Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday];
The value of a set variable is undefined until it is assigned a value. When you assign a set constant to a set variable, their base types must be compatible, i.e., they must be of the same type, subranges of the same type, or one of them must be a subrange of the other. Here are more assignments:
OddNum:= [1,3,5,7,9]; EvenNum:= [2,4,6,8]; ProgCodes:= [C..Assembly]; LLL:= [Assembly];
The empty set is a set with no members and is denoted by the constant []. You may assign this constant to any set variable of any base type, for example:
OddNum:= [];
The following are the main rules and restrictions that control the use of sets:
NewSet:= OldSet;
DaysArray = ARRAY[1..10] OF DaySet;
In addition to the membership operator IN, you can use the following operations with sets of compatible types:
The union of two sets S1 and S2 is a set whose elements are in either S1 or S2, or in both. The operator + is used for this operation, for example (using the previous declarations):
Alphabet:= Small + Capital;
The set Alphabet will thus contain both the lowercase and the uppercase letters.
Previous | Table of Contents | Next |
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement. |