INDEX, SCAN, VERIFY and the string tools
Three intrinsics answer the three questions a program asks of a string: where does this text start, where is the first character of this kind, and where does the text stop being what I expect. They all return a position counted from one, and they all return zero for "not found", which is the value to test before using the answer as a subscript.
INDEX
index(s, "quick") is the position where the substring starts in s, or zero when it is not there. The optional keyword back=.true. searches from the end and returns the last occurrence, the same keyword the array locators take.
The file search.f90:
program search_demo
implicit none
character(len=30) :: s
s = "the quick brown fox"
print *, "s : [", s, "]"
print *, "index quick :", index(s, "quick")
print *, "index o :", index(s, "o")
print *, "index o back:", index(s, "o", back=.true.)
print *, "index zebra :", index(s, "zebra")
end program search_demo
gfortran -Wall -Wextra -std=f2018 search.f90 -o search
./search
s : [the quick brown fox ]
index quick : 5
index o : 13
index o back: 18
index zebra : 0
The string was declared with thirty characters, so the closing bracket sits far to the right: the search includes the trailing blanks, which contain no match and change nothing. The o of brown is at thirteen and the one of fox at eighteen, and a substring that is not present gives zero.
Every occurrence
A search for all occurrences calls index on the rest of the string and adds the offset back. The loop below prints every o of a sentence:
program positions_demo
implicit none
character(len=30) :: s
integer :: pos, from
s = "the quick brown fox jumps over"
from = 1
do
pos = index(s(from:), "o")
if (pos == 0) exit
print *, "o at", from + pos - 1
from = from + pos
end do
end program positions_demo
gfortran -Wall -Wextra -std=f2018 positions.f90 -o positions
./positions
o at 13
o at 18
o at 27
s(from:) is a substring from the current position to the end, so the result of index is relative to it; adding from - 1 turns it back into a position in the whole string. The next search starts one place after the match found, which is what makes a doubled letter find both of its positions.
SCAN
scan(s, set) looks for the first character of s that is a member of the set and returns its position. It is the tool for skipping a class of characters: the first digit, the first letter, the first punctuation mark.
program scan_demo
implicit none
character(len=20) :: s
s = " 42 apples"
print *, "scan digit :", scan(s, "0123456789")
print *, "scan back :", scan(s, "0123456789", back=.true.)
print *, "verify set :", verify(s, " ")
print *, "verify bad :", verify("abc", "xyz")
end program scan_demo
gfortran -Wall -Wextra -std=f2018 scan.f90 -o scan
./scan
scan digit : 3
scan back : 4
verify set : 3
verify bad : 1
The first digit is the four at position three, and the last digit of the pair is the two at four. The set does not have to be sorted or complete: it is every character the search accepts, and a character outside it is skipped.
VERIFY
verify(s, set) is the complement: it returns the position of the first character that is not in the set. That is how a program skips leading blanks, which is the set of one space, or finds the first character that makes a record invalid. With back=.true. it finds the last character outside the set, which a right-trim does for blanks.
The output above shows both uses: verify(s, " ") is three because the first non-blank is the four, and verify("abc", "xyz") is one because the first character of the string is already outside the set.
Counting words
A word count is a loop over the characters that remembers whether it is inside a word. The file words.f90 counts the words of a line with leading and trailing blanks:
program words_demo
implicit none
character(len=60) :: line
integer :: i, words
logical :: in_word
line = " a stitch in time saves nine "
words = 0
in_word = .false.
do i = 1, len(line)
if (line(i:i) == " ") then
in_word = .false.
else if (.not. in_word) then
in_word = .true.
words = words + 1
end if
end do
print *, "line : [", line, "]"
print *, "words :", words
end program words_demo
gfortran -Wall -Wextra -std=f2018 words.f90 -o words
./words
line : [ a stitch in time saves nine ]
words : 6
The trailing blanks of the fixed length are part of the loop, and they do not add a word because the flag is already false when they arrive. The same counter works for any separator once the test line(i:i) == " " becomes a call to scan with a set of separators, which is the version a general reader of records needs.
Exercises
- Find the position of the first t and the last one in the sentence. Which keyword do you need for the second?
- Print every position of the letter e in s with the loop of positions.f90.
- Use scan to find the first letter of the alphabet in a string that starts with spaces and digits. Which set do you pass?
- Change the separator of the word count to a comma. How many words does "a,b,c" have with the new test?
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does
index(s, "brown")return when the word is at position 11?Answer
11
Positions count from one, and the answer is the start of the match.
-
What does INDEX return when the substring is not present?
Answer
0
Zero is the no-match value of the search intrinsics.
-
What does this program print?
program p implicit none character(len=9) :: s s = "abcabc" print *, index(s, "bc", back=.true.) end program pAnswer
5
The last bc starts at position five.
-
What is
scan(s, "0123456789")for?Answer
Finding the first character of s that is a digit
SCAN looks for a character from the set.
-
What is
verify(s, " ")for?Answer
Finding the first character that is not a blank
VERIFY looks for a character outside the set.
-
What does
verify("abc", "xyz")return?Answer
1
The first character is already outside the set.
-
In the loop that finds every occurrence, why is the result of INDEX added to
from - 1?Answer
The search was made on a substring, so the result is relative to it
The offset of the substring must be added back.
-
What does the word count test to start a new word?
Answer
A non-blank character while the flag says the loop is not in a word
A word starts at the first non-blank after a separator.
-
Which pair of intrinsics searches for a character of a set and for a character outside a set?
Answer
SCAN and VERIFY
SCAN accepts a character from the set, VERIFY rejects it.
-
What is printed by
print *, index("hello", "l")?Answer
3
The first l is the third character.
The programs on this page were built with GNU Fortran (Alpine 15.2.0) 15.2.0 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev