Programming Languages Need A Variety of Data Types in Order To Better Model/match The World
Programming Languages Need A Variety of Data Types in Order To Better Model/match The World
here we have
descriptors for
1-D and multi-D
arrays
Associative Arrays
• An associative array uses a key to map to the proper
location rather than an index
– keys are user-defined and must be stored in the data structure
itself
– this is basically a hash table
• Associative arrays are available in Java, Perl, and PHP,
and supported in C++ as a class and Python as a type
called a dictionary
– in Perl, associative arrays are implemented using a hash table
and a 32-bit hash value, but, at least initially, only a portion
of the hash value is used and stored, this is increased as
needed if the hash table grows
– in PHP, associative arrays are implemented as linked lists
with a hashing function that can point into the linked list
Record Types
• Heterogeneous aggregate of
Examples:
data elements
– elements referred to as fields or COBOL (nested structure in one
members definition)
– introduced in COBOL 01 EMPLOYEE-RECORD.
02 EMPLOYEE-NAME.
– incorporated into most
05 FIRST PICTURE IS X(10).
languages since then 05 MIDDLE PICTURE IS X(10).
• Java does not have a record 05 LAST PICTURE IS X(20).
type but uses the class construct 02 HOURLY-RATE PICTURE IS 99V99.
instead
– may be hierarchically structured Ada (nested through multiple definitions)
(nested) type Employee_Name_Type is record
First : String(1..10);
• Design Issues: Middle : String(1..10);
Last : String(1..10);
– how to build hierarchical end record;
structure type Employee_Record is record
– referencing of fields Employee_Name : Employee_Name_Type
Hourly_Rate : Float;
– record operations and end record;
implementations
Record Operations
• Assignment
– if both records are the same type
– allowed in Pascal, Ada, Modula-2, C/C++
• Comparison (Ada)
• Initialization (Ada, C/C++)
• Move Corresponding (COBOL)
– copies input record to output file while possibly performing some
modification
• To reference an individual element:
– COBOL uses OF as in First OF Emp-Name
– Ada uses “.” as in Emp_Rec.Emp_Name.First
– Pascal, Modula-2 same as Ada but also allow a With statement so that
variable names can be omitted
• with emp_record do
– begin
– first = …
– end;
– FORTRAN 90/95 use % sign as in Emp_Rec%Emp_Name%First
– PL/I and COBOL allow elliptical references where you only specify the
field name if the name is unambiguous
Record Implementation
• Similar to Arrays, requires a mapping function
– since fields are statically defined, mapping function is determined at
compile-time
– example:
A generic
type Foo is record compile-time
name : String(1..10); descriptor for
sex : char; a record is
salary : float; given to the right
end record;