Search code examples
fortranfortran77

What does the intrinsic function "INDEX" in Fortran 77 do?


I am studying a code and came across the following line:

NP = INDEX (PATH, ' ')-1

"NP" is declared as an integer variable and PATH as a character. The word "INDEX" stands for a Fortran function (it appears highlighted in the code).

What does it mean and what does it do? I've searched online for help but none of the results I found were satisfactory.

I did find a reference that says the INDEX feature is always written as "INDEX (STRING, SUBSTRING)", but it said nothing more; ergo, the doubt still prevails.

I'd be grateful if you could explain how the code line above works, or if you point me out in the direction of some good resources.


Solution

  • Since you specifically asked for the definition for FORTRAN 77, here is the corresponding part of the FORTRAN 77 Standard...

    Cl. 15.10 "Table of Intrinsic Functions":

    Notes for Table 5:

    [...]

    1. INDEX(a1,a2) returns an integer value indicating the starting position within the character string a1 of a substring identical to string a2. If a2 occurs more than once in a1, the starting position of the first occurrence is returned.

      If a2 does not occur in a1, the value zero is returned. Note that zero is returned if LEN(a1) < LEN(a2).

    And the corresponding line from Table 5:

    |                   |                 |          |        |         |                     |
    |                   |                 | Number of| Generic| Specific|       Type of       |
    |Intrinsic Function | Definition      | Arguments|  Name  | Name    | Argument | Function |
    |___________________|_________________|__________|________|_________|__________|__________|
    | [...] 
    |___________________|_________________|__________|________|_________|__________|__________|
    |                   |                 |          |        |         |          |          |
    |Index of           | Location of     |     2    |        | INDEX   | Character| Integer  |
    |  a Substring      | Substring a2    |          |        |         |          |          |
    |                   | in String a1    |          |        |         |          |          |
    |                   | See Note 1      |          |        |         |          |          |
    |___________________|_________________|__________|________|_________|__________|__________|
    

    So your line

    NP = INDEX (PATH, ' ')-1
    

    searches for the first occurrence of a blank () in the variable PATH and subtracts 1 from its position. This is then assigned to NP.