Re: Assumed-shape array of variable-length strings

On 5 juin, 15:09, fj <francois.j...@xxxxxxx> wrote:
On 5 juin, 10:48, Jovan Cormac <limu...@xxxxxxx> wrote:

I have a subroutine that accepts an array of variable-length strings of
variable size (assumed-shape array) and is to output all of those strings:

   SUBROUTINE routine1(strings)

     CHARACTER (LEN = *), INTENT(IN) :: strings(:)

This statement declares a vector of "fixed" length strings, the common
length of these strings being declared in the calling routine.

     INTEGER :: i

     WRITE(*,*) (strings(i), i = 1, SIZE(strings))

   END SUBROUTINE routine1

If I call this subroutine using e.g.

CALL routine1( (/ 'Hello', ' ', 'World!' /) )

This instruction is not legal : all the strings must have the same
length ! Your compiler has accepted it (extension) in padding the
shortest strings with spaces.

Correction : not legal in FORTRAN-95

I get the following output:

Hello         World!

As it turns out, the array "strings" is not actualy an array of
variable-length strings as I want it to be, but rather an array of
*fixed-length strings the length of the longest string passed*.

Therefore, the single character ' ' gets inflated to match the length of
the other two strings.

The obvious hack to go around this is to TRIM the strings before
outputting them:

     WRITE(*,*) (TRIM(strings(i)), i = 1, SIZE(strings))

However, in the above case, this of course results in:


being written, because the whitespace character gets trimmed down to

More promising looks the idea of getting the length of each passed
string and truncating before outputting:

     WRITE(*,*) (strings(i)(1:LEN(strings(i)), i = 1, SIZE(strings))

However, as mentioned above, the strings all actually have the same
internal length!

Is there any way to determine the "real" length of the strings passed
through the array? If not, is there maybe another solution to my problem?

Thanks in advance.

-- jovan