8.11 - Records
What records are and why they are useful
Records are data structures used to store collections of related data values. They are particularly helpful in programming because they allow you to keep different types of information together in one place, making it easier to manage and organise data.
Key features of records
- Different data types - Unlike arrays, which typically store items of the same data type, records can hold values with various data types, such as strings (text), integers (whole numbers), and Booleans (true or false values).
- Fields - Each item in a record is known as a field. When creating a record, you assign each field a data type and a field name, which describes the kind of data it will store.
- Fixed length - Records have a set structure, so you cannot add or remove fields after the record has been defined.
- Database context - In a database table, a record represents a single row of data, containing related information about one entity, such as a person or product.
Different programming languages implement records in slightly varied ways. For example, Python uses dictionaries (key-value pairs), while C uses structures (structs).
Creating a record structure
To use records, you first define their structure by specifying the fields, their data types, and their names. This creates a template that can then be used to store actual data.
Example of defining a record structure
Here is pseudocode that defines a record for storing recipe information:
Components of this record structure:
- This record is named
recipes. - It has four fields:
recipeNumber(for an integer ID),recipeName(for the recipe's title as a string),tested(a Boolean to show if it has been tried), andscore(an integer rating).
Once defined, this structure acts as a blueprint for creating multiple instances of the record.
Assigning values to records
After defining the record structure, you can create variables that use this structure and assign values to their fields. The values must match the data types specified in the structure.
Example of assigning records to variables
Here is pseudocode that creates three recipe records:
How this assignment works:
- Each variable (
recipe1,recipe2,recipe3) now holds a complete record. - The values are assigned in the order of the fields: integer for
recipeNumber, string forrecipeName, Boolean fortested, and integer forscore.
This approach keeps all related data for each recipe together, making it easier to handle than separate variables.
Accessing and modifying records
You can retrieve or change the data in a record using the variable name. To access a specific field, use dot notation (variable name followed by a dot and the field name).
Accessing whole records or individual fields
Here is pseudocode that prints a whole record and a specific field:
Output:
Modifying fields in a record
Fields can be updated as needed. Here is pseudocode that changes values and uses them:
Output:
Chicken Stir-fry has a score of 7
This shows how records allow flexible updates while keeping data organised.
Grouping records in arrays
If you have several records with the same structure, you can store them in an array (a data structure for holding multiple items in a list). This is useful for managing collections, like a list of recipes.
Creating an array of records
Here is pseudocode that creates an array and adds records to it:
Looping through an array of records
You can use loops to process the array and access fields with dot notation. Here is pseudocode that prints names of high-scoring recipes:
Output:
Pasta Bake