Previous | Table of Contents | Next |
Each object of type character has a fixed length, which is the number of characters that the string has. For example, the declaration
character (len = 7) :: string_7
declares the variable string_7 to be a character string of length 7. It is possible to have an array of character strings, all of the same length. The following declares string_array to be a 5 × 9 × 7 array of character strings of length 20:
character (len = 20), dimension (5,9,7) :: string_array
It is possible for a character string to have length 0.
Character dummy arguments may have their length designated as an asterisk, indicating that their length will be determined by the corresponding actual argument.
1.2.16.1. Character Parameters
A character constant may be given a name using the parameter attribute. As a simple example, the program hello prints a character parameter or named character constant, instead of a literal character constant:
program hello character (len = *), parameter :: & message = Hello, I am a computer. print *, message end program hello
Note that the name of the character parameter must be declared, just like a character variable, but the length may be declared as an asterisk indicating that the length is to be determined from the value of the string.
1.2.16.2. Character Constants
A character constant is enclosed in quotation marks (double quotes). It may have a kind parameter, which precedes the opening quote:
greek_μiκoνσσ
1.2.16.3. Substrings
A substring of a character string is any consecutive sequence of characters in the string. There is a convenient way to refer to any contiguous subsequence of characters of a character string. This is done by writing after any character variable or array element two integer expressions that give the positions of the first and last characters in the substring. These two expressions are separated by a colon and enclosed in parentheses. An example is string(k:m), where the values of k and m are positive integers less than or equal to the length of string and k[le]m. If k>m, the result is the null string. For example if c = crunch,
c (2 : 4) is run c (1 : 6) is crunch c (3 : 2) is the null string c (2 : 7) is illegal c (5 : 5) is c
The last example illustrates how to refer to a single character of a string.
1.2.16.4. Concatenation
The only built-in operation that can be performed on strings that produces a string result is concatenation. The symbol for concatenation is two slashes (//).
A structure is a collection of values, not necessarily of the same type. The objects that make up a structure are called its components. The components of a structure are identified by Fortran names.
An example of the use of a structure might be provided by a simple text editor, such as the one supplied with many BASIC programming language systems. Each line in a BASIC program consists of a line number and one or more statements. One way to do this is to have an object called line consisting of two components, an integer line_number, and a character string statement. The entire program would then be an array of these structures, one for each line.
The components of a structure may be arrays or other structures. The elements of an array may be a structure. The elements of an array may not be arrays, but this functionality can be achieved with an array whose elements are structures whose only component is an array or by a higher dimensional (rank) array.
1.2.17.1. Derived Types
As was mentioned previously, there are five intrinsic Fortran data types: integer, real, complex, logical, and character. A programmer may define a new data type, called a derived type. A derived type can be used only to define a structure. Conversely, a structure can occur in a program only as a value of some derived type.
A type definition begins with the keyword type, possibly followed by the private or public accessibility attribute, followed by two colons (::) and the name of the type being defined. The components of the type are given in the form of ordinary type declarations. The type definition ends with the keywords end type, followed by the name of the type being defined.
A definition of a type that would be useful for the BASIC editor is
type, public :: line integer :: line_number character (len = line_length) :: text end type line
where line_length is an integer parameter (named constant).
1.2.17.2. Declaring and Using Structures
Given the type definition for line, a variable new_line that could be used to represent one line of the program can then be declared by
type (line) :: new_line
As shown in this example, a variable is declared to be a derived type with a declaration that is similar to the declaration of a variable of intrinsic type, except that the name of the intrinsic type is replaced by the keyword type and the name of the type in parentheses.
An entire BASIC program to be edited could be represented by a single variable declared to be an array of values of type line:
type (line), dimension (max_lines) :: basic_program
1.2.17.3. Referencing Structure Components
A component of a structure is referenced by writing the name of the structure followed by a percent sign (%) and then the name of the component. Suppose new_line is a variable declared to be type line as shown previously. Then the line number of the line is referenced by the expression
new_line % line_number
1.2.17.4. Structure Constructors
Each derived-type declaration creates a structure constructor, whose name is the same as that of the derived type. For example, if you define a type named boa, you have a boa constructor. This constructor may be used much like a function to create a structure of the named type. The arguments are values to be placed in the individual components of the structure. For example, using the type line, a value for new_line may be assigned with the statement
new_line = line (previous_line_number + 1, LET A = B)
Previous | Table of Contents | Next |