Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>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.</p> <p>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.</p> <p>Here is my version of the function that checks for alternate white space characters at the end besides spaces.</p> <pre><code> 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 </code></pre> <p>If there are other characters in the "garbage" section this still won't work completely.</p> <p>Assuming that it does work for your data, however, you can then change your write statement to look like this:</p> <pre><code>write(*,*) matname(matidx)(1:strlen(matname(matidx))) </code></pre> <p>and it will print out just the actual string.</p> <p>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.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload