Home > Archive > Fortran > March 2008 > Comparing two strings
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Comparing two strings
|
|
| relaxmike 2008-03-14, 8:15 am |
| Hi all fortran gurus,
I am currently testing the iso_varying_string.f90 module and I am
facing a
unexpected behaviour of the string comparison system of fortran.
I understand that the current behaviour is that, before comparing the
equality of two strings, they are trimmed so that all trailing blanks
are deleted.
This program exhibits the problem :
program testcharstring
implicit none
character (len=3D9), parameter :: string1 =3D "my string"
character (len=3D10), parameter :: string2 =3D "my string "
logical :: equals
equals =3D string1=3D=3Dstring2
print * , "Equals :", equals
end program testcharstring
It prints :
Equals : T
It is strange, since the two strings does not even have the same
length !
Am I dreaming or it is the "regular" behaviour ?
If so, I will make my new version of the module behaves so that the
comparison algorithm is based on (what I expected it was ):
- first compare the length,
- second, make a loop over the characters to check that they are all
the same.
Just to make sure (my mind was so that everything is
possible !),
I tested the Tcl comparison system and it works as I expected (ouf !).
Does any other language implements the string comparison the way that
fortran does ?
Any comments ?
Best regards,
Micha=EBl
| |
| Dr Ivan D. Reid 2008-03-14, 8:15 am |
| On Fri, 14 Mar 2008 03:37:30 -0700 (PDT), relaxmike <michael.baudin@gmail.com>
wrote in <860e17c5-94de-48ab-876c-aafc72ef89da@e39g2000hsf.googlegroups.com>:
> Hi all fortran gurus,
>
> I am currently testing the iso_varying_string.f90 module and I am
> facing a
> unexpected behaviour of the string comparison system of fortran.
> I understand that the current behaviour is that, before comparing the
> equality of two strings, they are trimmed so that all trailing blanks
> are deleted.
> This program exhibits the problem :
> program testcharstring
> implicit none
> character (len=9), parameter :: string1 = "my string"
> character (len=10), parameter :: string2 = "my string "
> logical :: equals
> equals = string1==string2
> print * , "Equals :", equals
> end program testcharstring
> It prints :
> Equals : T
> It is strange, since the two strings does not even have the same
> length !
> Am I dreaming or it is the "regular" behaviour ?
No, you are dreaming. :-) The shorter string is extended with blanks.
CVF Language Reference sez:
"Character operands are compared one character at a time, in order,
starting with the first character of each operand. If the two character
operands are not the same length, the shorter one is padded on the right
with blanks until the lengths are equal; for example:
'ABC' .EQ. 'ABC '
'AB' .LT. 'C'
The first relational expression has the value .TRUE. even though the
lengths of the expressions are not equal, and the second has the value
..TRUE. even though 'AB' is longer than 'C'."
--
Ivan Reid, School of Engineering & Design, _____________ CMS Collaboration,
Brunel University. Ivan.Reid@[brunel.ac.uk|cern.ch] Room 40-1-B12, CERN
KotPT -- "for stupidity above and beyond the call of duty".
| |
| Arjen Markus 2008-03-14, 8:15 am |
| On 14 mrt, 11:37, relaxmike <michael.bau...@gmail.com> wrote:
> Hi all fortran gurus,
>
> I am currently testing the iso_varying_string.f90 module and I am
> facing a
> unexpected behaviour of the string comparison system of fortran.
> I understand that the current behaviour is that, before comparing the
> equality of two strings, they are trimmed so that all trailing blanks
> are deleted.
> This program exhibits the problem :
>
> program testcharstring
> =A0 implicit none
> =A0 character (len=3D9), parameter :: string1 =3D "my string"
> =A0 character (len=3D10), parameter :: string2 =3D "my string "
> =A0 logical :: equals
> =A0 equals =3D string1=3D=3Dstring2
> =A0 print * , "Equals :", equals
> end program testcharstring
>
> It prints :
>
> Equals : T
>
> It is strange, since the two strings does not even have the same
> length !
> Am I dreaming or it is the "regular" behaviour ?
>
> If so, I will make my new version of the module behaves so that the
> comparison algorithm is based on (what I expected it was ):
> - first compare the length,
> - second, make a loop over the characters to check that they are all
> the same.
>
> Just to make sure (my mind was so that everything is
> possible !),
> I tested the Tcl comparison system and it works as I expected (ouf !).
>
> Does any other language implements the string comparison the way that
> fortran does ?
> Any comments ?
>
> Best regards,
>
> Micha=EBl
A better way to think about the comparison is that the strings
are extended to the same length by adding trailing blanks to the
shorter one. (Whether the compiler will actually produce code
that does this, is another matter :)).
But the behaviour as you see it is completely standard.
Regards,
Arjen
| |
| Terence 2008-03-14, 7:32 pm |
| If you compare the strings FIRST and obtain an "equal" result, you can
THEN compare the original lengths to detect whether one of the two
strings started out with more trailing blanks than the other. This
will resolve your problem.
| |
| relaxmike 2008-03-19, 4:32 am |
| Thank you for all your messages.
Since this behaviour is standard fortran, I have to do my own
algorithm.
It is really simple anyway.
Micha=EBl
| |
|
| "Terence" <tbwright@cantv.net> wrote in message
news:4d08cc08-4109-4cbb-b4d5-3756ce353f27@s12g2000prg.googlegroups.com...
> If you compare the strings FIRST and obtain an "equal" result, you can
> THEN compare the original lengths to detect whether one of the two
> strings started out with more trailing blanks than the other. This
> will resolve your problem.
Quicker to compare lengths first.
| |
|
| "relaxmike" <michael.baudin@gmail.com> wrote in message
news:860e17c5-94de-48ab-876c-aafc72ef89da@e39g2000hsf.googlegroups.com...
>I am currently testing the iso_varying_string.f90 module and I am
>facing a
>unexpected behaviour of the string comparison system of fortran.
>I understand that the current behaviour is that, before comparing the
>equality of two strings, they are trimmed so that all trailing blanks
>are deleted.
If that's what you want, use LEN_TRIM.
All characters are compared.
If the strings are of different length, the shorter is considered
as being padded with blanks.
|
|
|
|
|