FORTRAN:使用字符作为可变数组名
FORTRAN: Use character as changeable array name
我正在尝试编写一个子例程以某种方式访问数组。
子例程的一个输入参数是一个字符,其中包含需要访问的数组的名称。这是一个相当简化的代码示例,说明我通常如何想象它是如何工作的:
PROGAM prog
real, dimension(3,3) :: array1(3,3)
real, dimension(3,3) :: array2(3,3)
real value1
real value2
... fill 'array1' and 'array2'...
call sub(array1,2,2,value1)
call sub(array2,2,2,value2)
... do something with 'value1' and 'value2'...
END
SUBROUTINE sub(name,x,y,out)
character(len=*), intent(in) :: name
integer, intent(in) :: x
integer, intent(in) :: y
real, intent(out) :: out
out = name(x,y)
RETURN
END
我想要的是根据参数中的要求访问 array1(2,2)
的子例程,并将此值 return 访问 value1
。然后访问array2(2,2)
和return这个值到value2
。上面的代码片段不起作用——难怪。如何将 name(x,y)
分别替换为 array1(x,y)
array2(x,y)
?
非常感谢和最诚挚的问候!
如果数组是在调用者中声明的,则不必使用数组名称来获取其内容。然后你使用正常的参数关联。
你可以把你的子程序写成
subroutine sub(a,x,y,out)
integer, intent(in) :: x, y
real, intent(out) :: out
real, dimension(:,:), intent(in) :: a
out = a(x,y)
end subroutine sub
在主程序中做起来更简单
value1 = array1(2,2)
value2 = array2(2,2)
我会推荐 Fortran 教科书,甚至看一看 http://en.wikipedia.org/wiki/Fortran_95_language_features。
编辑:
您也可以使用 SELECT CASE 语句,如下所示:
subroutine bar(c, i, j, out)
character(len=*), intent(in) :: c
integer, intent(in) :: i,j
real, intent(out) :: out
select case (trim(c))
case ("array1")
out = array1(i,j)
case ("array2")
out = array2(i,j)
case default
stop "Argument is bletchful"
end case
end subroutine bar
与直接使用数组相比,我完全不知道这样做有什么好处。
这是你想要的吗?
select case (array_name)
case ("array_dog")
array_ptr => array_dog
case ("array_cat")
array_ptr => array_cat
end select
其中array_name
是字符变量,array_dog
和array_cat
是模块中声明的数组(需要target
属性),array_ptr
是另一个具有 pointer
属性的数组。
我正在尝试编写一个子例程以某种方式访问数组。 子例程的一个输入参数是一个字符,其中包含需要访问的数组的名称。这是一个相当简化的代码示例,说明我通常如何想象它是如何工作的:
PROGAM prog
real, dimension(3,3) :: array1(3,3)
real, dimension(3,3) :: array2(3,3)
real value1
real value2
... fill 'array1' and 'array2'...
call sub(array1,2,2,value1)
call sub(array2,2,2,value2)
... do something with 'value1' and 'value2'...
END
SUBROUTINE sub(name,x,y,out)
character(len=*), intent(in) :: name
integer, intent(in) :: x
integer, intent(in) :: y
real, intent(out) :: out
out = name(x,y)
RETURN
END
我想要的是根据参数中的要求访问 array1(2,2)
的子例程,并将此值 return 访问 value1
。然后访问array2(2,2)
和return这个值到value2
。上面的代码片段不起作用——难怪。如何将 name(x,y)
分别替换为 array1(x,y)
array2(x,y)
?
非常感谢和最诚挚的问候!
如果数组是在调用者中声明的,则不必使用数组名称来获取其内容。然后你使用正常的参数关联。
你可以把你的子程序写成
subroutine sub(a,x,y,out)
integer, intent(in) :: x, y
real, intent(out) :: out
real, dimension(:,:), intent(in) :: a
out = a(x,y)
end subroutine sub
在主程序中做起来更简单
value1 = array1(2,2)
value2 = array2(2,2)
我会推荐 Fortran 教科书,甚至看一看 http://en.wikipedia.org/wiki/Fortran_95_language_features。
编辑:
您也可以使用 SELECT CASE 语句,如下所示:
subroutine bar(c, i, j, out)
character(len=*), intent(in) :: c
integer, intent(in) :: i,j
real, intent(out) :: out
select case (trim(c))
case ("array1")
out = array1(i,j)
case ("array2")
out = array2(i,j)
case default
stop "Argument is bletchful"
end case
end subroutine bar
与直接使用数组相比,我完全不知道这样做有什么好处。
这是你想要的吗?
select case (array_name)
case ("array_dog")
array_ptr => array_dog
case ("array_cat")
array_ptr => array_cat
end select
其中array_name
是字符变量,array_dog
和array_cat
是模块中声明的数组(需要target
属性),array_ptr
是另一个具有 pointer
属性的数组。