Fortran 77中的字符串数组 [英] Array of Strings in Fortran 77

查看:2716
本文介绍了Fortran 77中的字符串数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于Fortran 77的问题,我找不到一个解决方案。

I've a question about Fortran 77 and I've not been able to find a solution.

我试图存储一个字符串数组定义为

I'm trying to store an array of strings defined as the following:

character matname(255)*255

Wich是一个长度为255的255字符串数组。

Wich is an array of 255 strings of length 255.

后来我从文件中读取了名称列表,我设置数组的内容如下:

Later I read the list of names from a file and I set the content of the array like this:

matname(matcount) = mname

EDIT:实际上 mname 的值被编码为 mname ='AIR'类型字符* 255 ,它是一个函数 matadd 执行上一行。

Actually mname value is harcoded as mname = 'AIR' of type character*255, it is a parameter of a function matadd() wich executes the previous line. But this is only for testing, in the future it will be read from a file.

稍后我想用以下方式打印:

Later on I want to print it with:

write(*,*) matname(matidx)

但是它似乎打印所有的255个字符,它打印我分配的字符串和大量的垃圾。

But it seems to print all the 255 characters, it prints the string I assigned and a lot of garbage.


  • 这是我的问题,我如何知道存储的字符串的长度?

  • 我应该如何获得另一个数组?

  • 如何知道读取的字符串的长度?

感谢。

推荐答案

Timotei发布的功能你的字符串的长度,只要你感兴趣的字符串的部分只包含空格,如果你在程序中分配的值应该是真的FORTRAN应该初始化变量为空和字符

The function Timotei posted will give you the length of the string as long as the part of the string you are interested in only contains spaces, which, if you are assigning the values in the program should be true as FORTRAN is supposed to initialize the variables to be empty and for characters that means a space.

但是,如果您从文件中读取,则可以在行末尾选择其他控制字符(特别是回车和/或换行字符,\r和/或\\\
,具体取决于您的操作系统)。你也应该把那些在函数中得到正确的字符串长度。

However, if you are reading in from a file you might pick up other control characters at the end of the lines (particularly carriage return and/or line feed characters, \r and/or \n depending on your OS). You should also toss those out in the function to get the correct string length. Otherwise you could get some funny print statements as those characters are printed as well.

这是我的版本的函数,用于检查除空格以外的空白字符

Here is my version of the function that checks for alternate white space characters at the end besides spaces.

  function strlen(st)
  integer           i,strlen
  character         st*(*)
  i = len(st)
  do while ((st(i:i).eq.' ').or.(st(i:i).eq.'\r').or.
 +  (st(i:i).eq.'\n').or.(st(i:i).eq.'\t'))
    i = i - 1
  enddo
  strlen = i
  return
  end

在垃圾部分有其他字符,这仍然不能完全工作。

If there are other characters in the "garbage" section this still won't work completely.

假设它对你的数据有效,然后你可以改变你的写语句如下:

Assuming that it does work for your data, however, you can then change your write statement to look like this:

write(*,*) matname(matidx)(1:strlen(matname(matidx)))

,它将只打印实际的字符串。

and it will print out just the actual string.

至于是否应该使用另一个数组来保存字符串的长度,这取决于你。 strlen()函数是O(n),而查找表中的长度是O(1)。如果你发现自己经常计算这些静态字符串的长度,它可以提高性能,计算长度一次,当它们被读入,存储在一个数组,并查找它们,如果你需要它们。但是,如果你没有注意到减速,我不会担心。

As to whether or not you should use another array to hold the lengths of the string, that is up to you. the strlen() function is O(n) whereas looking up the length in a table is O(1). If you find yourself computing the lengths of these static strings often, it may improve performance to compute the length once when they are read in, store them in an array and look them up if you need them. However, if you don't notice the slowdown, I wouldn't worry about it.

这篇关于Fortran 77中的字符串数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆