Re: compare two dynamic string



TC wrote:
> Rich Townsend ha scritto:
>> TC wrote:
>>
>>> How compare two dynamic string?
> character,allocatable :: Parola (:)
> character,allocatable :: P (:)
>
> ??? if (Parola < P) then
> and = >

First of all, what you've got here is arrays of characters
and not strings. If you think the two concepts are nearly
identical, I agree. But in Fortran there are a lot of big
and mostly unnecessary differences.

There are several problems with using arrays of characters
instead if strings. For example, you can not assign strings
to arrays or vice-versa without extra steps. A common
way is as others have already shown:

P = transfer("some data!", P)

While it may seem strange to have P in both places above,
it's necessary. The P argument to transfer is neither evaluated
nor altered, so it could have been Parola instead, since Parola
has the same type and attributes. In fact the second argument
to TRANSFER can be anything with the same type and
attributes as you want to convert the first argument to have.

Now, the above assumes that P has been allocated to be
the same size as "some data!". You will have to count (or
use the LEN intrinsic in the ALLOCATE statement, but that
requires you to write the literal out twice).

Now, assuming you've managed to put data into your two
arrays, your question was how to compare them. We've
already been treated to a minimal, and completely non-general
approach to the problem. If your "dynamic strings" always
happen to be exactly 10 characters in length, then use David
Frank's solution.

I could give you a general solution to the problem as asked, but
at this point I suspect that what you really want is some kind of
string rather than an array anyway. We'll all need more detail
about what you're doing to be able to help more.

--
J. Giles

"I conclude that there are two ways of constructing a software
design: One way is to make it so simple that there are obviously
no deficiencies and the other way is to make it so complicated
that there are no obvious deficiencies." -- C. A. R. Hoare


.



Relevant Pages