如何在Fortran中获取派生类型的组件的数量,名称和值

时间:2017-06-20 12:09:41

标签: fortran fortran95

我是Fortran的新手。我想开发一个子程序,它将指定的变量信息打印到Fortran 95中的一个大型派生类型。为简化起见,假设我们有一个派生类型声明和assigmnent如下:

Booktype

我希望我的程序输出为文本文件,如下所示:

  

预订%标题,哈利波特

     

预订%作者,JK罗琳

     

预订%subject%genre,Fantasy

     

预订%主题%maindude,Ron Weasley

为了达到这个目的,我相信这就是我需要做的事情:

  • 确定每个级别的派生类型中的字段数。例如Booktype%SubjectType中的字段数为3. {{1}}中的字段数为2.
  • 找到一种方法将字段'number'与字段名称相关联(可能使用指针?)
  • 遍历所有字段编号并获取其名称和值。

我的问题是双重的。首先是我的方法正确/它会导致预期的结果吗?第二个如何完成此过程的第1步。也就是说,如何在每个级别获取派生类型中的字段数?

1 个答案:

答案 0 :(得分:2)

我没有按照问题中提出的索引号识别数据类型/字段,而是使用了一个非常基本的“键/值容器”类 1 来完成任务。我们的想法是使用一些通用的派生类型,这些类型可以重复使用,以创建任意数量的数据字段来表示各种书籍信息,并且您可以轻松访问(和/或打印)。

! Start off with a generic DT to represent character string data fields.
type :: Field
    character(len=:), allocatable :: type  !<- A category such as 'book'
    character(len=:), allocatable :: key   !<- Such as 'title' or 'author'
    character(len=:), allocatable :: val   !<- Such as 'Harry Potter' or 'JK Rowling'
end type

! It is simplest to create/use multiple Fields directly in the BookType.
type :: BookType
    character(len=8) :: type = 'BookType'
    type(Field), dimension(:), allocatable :: fields
    integer :: size = 0
    contains
        procedure, pass :: valueFromKey
end type

! Create a container to hold each BookType created.
type :: BookCollection
    type(BookType), dimension(:), allocatable :: book
end type

我将提供函数valueFromKey作为如何返回与给定键对应的值的示例。

function valueFromKey(self, key) 
    implicit none
    class(BookType) :: self
    character(*), intent(in) :: key
    character(len=:), allocatable :: valueFromKey
    integer :: i
    logical :: val_is_found

    val_is_found = .False.
    do i = 1,self%size 
        if (trim(key) == self%fields(i)%key) then
            valueFromKey = self%fields(i)%val
            val_is_found = .True.
            exit
        endif
    enddo

    if (.not. val_is_found) then
        valueFromKey = "None"
    endif
end function valueFromKey

将上述所有代码放入模块中。我遗漏了一些辅助函数,因为它们没有必要回答问题并提供有效的解决方案。但是,您将看到下面的示例程序中使用的其中一个(newBook)。请注意,循环字段会打印所有现有数据,这些数据可能因书而异。 OTH,请求valueFromKey任何不在图书数据中的密钥将返回字符串'None'。

! Example usage:
program main
    use BookModule
    implicit none
    integer :: i, j
    character(len=6) :: num
    type(BookCollection) :: MyBooks

    allocate(MyBooks%book(2))
    MyBooks%book(1) = newBook(keys=['title', 'author', 'date', 'genre', 'lead'], values=["Harry Potter", "JK Rowling", "1997", "Fantasy", "Ron Weasley"])
    MyBooks%book(2) = newBook(keys=['title', 'author', 'lead'], values=["1984", "George Orwell", "Winston Smith"])

    print *, "LOOP OVER BOOK COLLECTION"
    do i = 1, size(MyBooks%book)
        write(num, '(i6)') i
        print *, "Item ", adjustl(num)
        do j = 1, MyBooks%book(i)%size 
            print *, MyBooks%book(i)%type, ", ", MyBooks%book(i)%fields(j)%type, ", ", MyBooks%book(i)%fields(j)%key, ", ", MyBooks%book(i)%fields(j)%val
        enddo
        print *
    enddo

    print *, "GET FIELD VALUE FROM KEY"
    print *, " Title:  ", MyBooks%book(1)%valueFromKey('title')
    print *, " Author: ", MyBooks%book(1)%valueFromKey('author')
    print *, " Date:   ", MyBooks%book(1)%valueFromKey('date')
    print *
    print *, " Title:  ", MyBooks%book(2)%valueFromKey('title')
    print *, " Author: ", MyBooks%book(2)%valueFromKey('author')
    print *, " Date:   ", MyBooks%book(2)%valueFromKey('date')
end program main

示例输出:

 LOOP OVER BOOK COLLECTION
 Item 1
 BookType, book, title, Harry Potter
 BookType, book, author, JK Rowling
 BookType, book, date, 1997
 BookType, subject, genre, Fantasy
 BookType, subject, lead, Ron Weasley

 Item 2
 BookType, book, title, 1984
 BookType, book, author, George Orwell
 BookType, subject, lead, Winston Smith

 GET FIELD VALUE FROM KEY
  Title:  Harry Potter
  Author: JK Rowling
  Date:   1997

  Title:  1984
  Author: George Orwell
  Date:   None

1 这里提到的基本“键/值容器”类缺少任何散列/映射/类字典功能;查找只能通过循环数据成员才能找到密钥,然后获取相应的值。它实际上只适用于中小型数据集。