String matching: Difference between revisions

Line 1,120:
 
=={{header|Fortran}}==
Fortran does not offer a string type, but since F77 it has been possible to use a CHARACTER variable, of some specified size, whose size may be accessed via the LEN function. When passed as a parameter, a secret additional parameter specifies its size and so string-like usage is possible. Character matching is case sensitive, and, trailing spaces are ignored so that "xx" and "xx " are deemed equal. The function INDEX(text,target) determines the first index in ''text'' where ''target'' matches, and returns zero if there is no such match. Unfortunately, the function does not allow the specification of a starting position for a search, as to find any second and further matches. One must specify something like <code>INDEX(text(5:),target)</code> to start with position five, and then deal with the resulting offsets needed to relate the result to positions within the parameter. On the other hand, since there is no "length" conjoined to the text such substring selections can be made without copying the text to a work area, unlike the <code>copy(text,start,stop)</code> equivalent of Pascal for example. Some Fortran compilers ''do'' offer a starting point, and also an option to search backwards from the end, but these facilities are not guaranteed. Similarly, INDEX is only made available for CHARACTER searching, even though it could easily be generalised to other types.
 
A second problem is presented by the possibility that a logical expression such as <code>L.LT.0 .OR. ''etc.''</code> will always or might possibly or in certain constructions but not others be fully evaluated, which is to say that the ''etc'' will be evaluated even though L < 0 is ''true'' so that the result is determined. And in this case, evaluating the ''etc'' will cause trouble because the indexing won't work! To be safe, therefore, a rather lame two-stage test is required - though optimising compilers might well shift code around anyway.
1,220

edits