Basic Pascal Tutorial/Chapter 5/Records: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: Basic Pascal Tutorial/Chapter 5/Records
| Old | New | Line | |
|---|---|---|---|
| 1 | + | 5E - Records (author: Tao Yue, state: unchanged) | |
| 2 | + | ||
| 3 | + | A record allows you to keep related data items in one structure. If you want information about a person, you may want to know name, age, city, state, and zip. | |
| 4 | + | ||
| 5 | + | To declare a record, you'd use: | |
| 6 | + | ||
| 7 | + | ``` pascal | |
| 8 | + | TYPE | |
| 9 | + | TypeName = record | |
| 10 | + | identifierlist1 : datatype1; | |
| 11 | + | ... | |
| 12 | + | identifierlistn : datatypen; | |
| 13 | + | end; | |
| 14 | + | ``` | |
| 15 | + | ||
| 16 | + | For example: | |
| 17 | + | ||
| 18 | + | ``` pascal | |
| 19 | + | type | |
| 20 | + | InfoType = record | |
| 21 | + | Name : string; | |
| 22 | + | Age : integer; | |
| 23 | + | City, State : String; | |
| 24 | + | Zip : integer; | |
| 25 | + | end; | |
| 26 | + | ``` | |
| 27 | + | ||
| 28 | + | Each of the identifiers `Name, Age, City, State`, and `Zip` are referred to as fields. You access a field within a variable by: | |
| 29 | + | ||
| 30 | + | ``` pascal | |
| 31 | + | VariableIdentifier.FieldIdentifier | |
| 32 | + | ``` | |
| 33 | + | ||
| 34 | + | A period separates the variable and the field name. | |
| 35 | + | ||
| 36 | + | There's a very useful statement for dealing with records. If you are going to be using one record variable for a long time and don't feel like typing the variable name over and over, you can strip off the variable name and use only field identifiers. You do this by: | |
| 37 | + | ||
| 38 | + | ``` pascal | |
| 39 | + | WITH RecordVariable DO | |
| 40 | + | BEGIN | |
| 41 | + | ... | |
| 42 | + | END; | |
| 43 | + | ``` | |
| 44 | + | ||
| 45 | + | Example: | |
| 46 | + | ||
| 47 | + | ``` pascal | |
| 48 | + | with Info do | |
| 49 | + | begin | |
| 50 | + | Age := 18; | |
| 51 | + | ZIP := 90210; | |
| 52 | + | end; | |
| 53 | + | ``` | |
| 54 | + | ||
| 55 | + | --- | |
| 56 | + | *From the Free Pascal wiki, page "Basic Pascal Tutorial/Chapter 5/Records", as it stood on 2022-08-20 after a change by Kai Burghardt, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).* |
