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)
     IMPLICIT NONE

     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))

     RETURN
   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:

HelloWorld!

being written, because the whitespace character gets trimmed down to
nothing.

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



.



Relevant Pages

  • Re: similar Perl data structure?
    ... strings in a file like: ... As far as I can tell, the OP doesn't explicitly require storing the lines in memory before outputting. ... For some recommended resources on the net, again, check the C FAQ, Question 18.9. ... Well he did mention he stored the string in an array, so I assumed he needs to store them. ...
    (comp.lang.c)
  • Re: K&R2 Secition 5.9 - major blunders
    ... Each element of b doesn't point to a 20 element array of int. ... This mistake is crucial because ... my explanation is really the qualities of something else: ... > The use of the array of pointers is to store the strings. ...
    (comp.lang.c)
  • Re: K&R2 Secition 5.9 - major blunders
    ... Each element of b doesn't point to a 20 element array of int. ... This mistake is crucial because ... my explanation is really the qualities of something else: ... > The use of the array of pointers is to store the strings. ...
    (comp.lang.c)
  • Re: How to convert Delphi Record to C#?
    ... TMyJob ), or is it some custom streaming mechanism? ... Are the strings pre- or post unicode? ...    Willem van Rumpt ... Here is the code that saves to array, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Returning array of strings through callback from unmanaged to
    ... Is the array of strings a jagged array or fixed length array? ... It is imperative that I am able to return the array of strings from the ... I have control over how many strings I want the unmanaged code to return ... What I want to accomplish is to pass a callback function to unmanaged ...
    (microsoft.public.dotnet.framework.compactframework)