next up previous contents
Next: I/O Up: CHARACTER data Previous: REAL and INTEGER variables

CHARACTER variables

Character variable and array names follow the same rules as for reals and integers. All have to be explicitly declared using statements like:-
CHARACTER*7 TEXT,STRINGS(5,3)
CHARACTER*80 LINE
There is no implicit rule as there is for reals and integers (i.e. I...N are integer). The *7 and *80 indicate the number of

characters that each variable, or array element contains. So in the above example, every element of the array STRINGS can hold 7 characters. Assignment statements take the normal form e.g.

 
        CHARACTER*5  STR1,STR2
         
        .
        .
        .
         
        STR1='...'
        STR2='1234567'
         
        .
        .
        .
         
        STR2=STR1
If the capacity of the variable is different to the length of the string being assigned to it (as is the case with the first two assignments above) then the string is adjusted by adding spaces or removing characters at the right hand end. So, before the last line above STR1 would have '... ' and STR2 would have '12345' There is only one operator that can be used to combine variables and constants. It is the concatenation operater // which simply joins the two strings together. So the expression 'ABC D'//'12 34' would produce 'ABC D12 34'. Logical expressions such as LINE.EQ.TEXT and KCHAR.EQ.'A' are legal. If the two strings are of different length then the shorter is extended on the right with spaces before testing. The inequality relational operators .LE.,.LT.,.GE. and .GT. can be used and follow the usual alphabetical ordering e.g. 'A'.LT.'B' is true and 'XYY'.GT.'XYZ' is false. For non-alphabetic characters the ordering follows the ASCII code convention and in particular ' ' is less than '0' is less than 'A'. Unfortunately lower case letters are greater than all upper case i.e. "Z" is less than 'a'.

A substring of a variable or array element can be selected by adding a SUBSTRING REFERENCE using the form:-

(e1:e2)
where e1 and e2 are expressions converted to integer if necessary, which identify the first and last character positions. So if STR contains 'ABCDE' then STR(3:4) would select the substring 'CD'. The substring construct can be used to extract characters from a variable or an array element or to indicate the portion of the string that is to be replaced. In the substring reference construction, both e1 and e2 are optional. If e1 is omitted e1=1 is assumed, while if e2 is omitted e2=length of string is assumed. So the substring reference:-
(:)
specifies the complete string. For an array element the index comes before the substring reference i.e.:-
array-name(index)(e1:e2)
Shown below are some examples of substring use:-

 
        CHARACTER*6   CHARS(5),ALPHA
 
.
.
.
 
        ALPHA='ABCDEF'
        ALPHA(3:4)='XY'
        CHARS(4)='123456'
        CHARS(4)(4:6)=ALPHA(2:4)
100     CONTINUE
 
        ALPHA='GH'//ALPHA(2:2)//CHARS(4)
200     CONTINUE
When statement 100 is reached, ALPHA contains 'ABXYEF' and CHARS(4) has '123BXY', by statement 200 ALPHA has 'GHB123'
next up previous contents
Next: I/O Up: CHARACTER data Previous: REAL and INTEGER variables
n west (APC)
2000-03-08