col_index Function

private pure function col_index(line, name) result(idx)

Return 1-based position of whitespace-separated token equal to name, or -1.

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: line

Line of column names.

character(len=*), intent(in) :: name

Column name to find.

Return Value integer(kind=int32)


Source Code

   pure function col_index(line, name) result(idx)
      !! Return 1-based position of whitespace-separated token equal to `name`, or -1.
      ! Arguments
      character(len=*), intent(in) :: line
         !! Line of column names.
      character(len=*), intent(in) :: name
         !! Column name to find.
      integer(int32) :: idx, col, i, s, L
      idx = -1
      col = 0
      i = 1
      L = len_trim(line)
      do
         do while (i <= L)
            if (line(i:i) /= ' ' .and. line(i:i) /= char(9)) exit
            i = i + 1
         end do
         if (i > L) exit
         s = i
         do while (i <= L)
            if (line(i:i) == ' ' .or. line(i:i) == char(9)) exit
            i = i + 1
         end do
         col = col + 1
         if (line(s:i - 1) == trim(name)) then
            idx = col
            return
         end if
      end do
   end function col_index