Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

String Comparisons

gelarimer
Beginner
2,229 Views

When comparing two character strings of different lengths with Relational Operators (not Lexical functions), does the Fortran standardcall for comparing the the shorter string as if it were extended to the right with blanks to the length of the longer string? For example,

CHARACTER(10) cText

! cText all blanks
cText = ' '

! compare cText to a single blank
! thisevaluates to .TRUE.
if(cText == ' ')

Will the result of this comparison always be .TRUE.even on different processors because the Fortram standard requires it, or is this just something that occurs with IVF?

On page 384/385 of 'Fortran 90/95 for Scientist and Engineers', Chapman states 'If two strings are the same all the way to the end of one of them, then the other string is considered the larger of the two.' Chapman gives an example: 'AAAA' > 'AAA'but does not explicitly say that the shorter string is padded with a blank. One could assume (based on Chapman) that comparing a10 blank string with a 1 blank string would yield the following: ' ' > ' ', but this does not agree with the if(cText == ' ') evaluating to .TRUE. above.

Thanks for any information.

0 Kudos
5 Replies
Steven_L_Intel1
Employee
2,229 Views
Yes, the standard says that the shorter string is extended with blanks. Here's the text from F95:

For a character relational intrinsic operation, the operands are compared one character at a time in order, beginning with the first character of each character operand. If the operands are of unequal length, the shorter operand is treated as if it were extended on the right with blanks to the length of the longer operand.
0 Kudos
gelarimer
Beginner
2,229 Views
Thank you for the information and reply.
0 Kudos
jimdempseyatthecove
Honored Contributor III
2,229 Views

So then according to the above descriptionan inconsistancy may appear if the longer string contains characters of value less than space (control characters or NULL).

The following illustrates a string with a control-A

program CharTest
implicit none
character(5) :: StringA = 'ABCDE'
character(7) :: StringB = 'ABCDE' // CHAR(1) // 'X'
if(StringA .gt. StringB) write(*,*) '(StringA .gt. StringB)'
if(StringA .lt. StringB) write(*,*) '(StringA .lt. StringB)'
if(StringA .eq. StringB) write(*,*) '(StringA .eq. StringB)'
end program CharTest

Displays "(StringA .gt. StringB)'

Therefore cautionmust be observedif your strings contain characters of less than space. (e.g. RETURN, LINEFEED, TAB, NULL, SmilyFaces, ...)

Jim Dempsey

0 Kudos
Steven_L_Intel1
Employee
2,229 Views
Why is this inconsistent? I omitted the text that describes the actual comparison, which is based on the position of each character in the collating sequence for the character kind. Characters such as CR, LF, TAB and NUL all have defined positions in the ASCII collating sequence (which in Intel Fortran is the one used for kind "default character". Blank is not necessarily "less than" other characters, and the Fortran standard even says so when it discusses collating sequences.

Yes, if you might have non-printing characters in your string then you have to be careful, not only with comparisons but also operations such as TRIM. TRIM does not consider NUL to be a trimmable character, for example.
0 Kudos
jimdempseyatthecove
Honored Contributor III
2,229 Views

It is not inconsistent with the specification.

It is inconsistent withthe programmersrational expectation of the stringcompare.

i.e. the expectation that "something" is greater than "nothing"
or conversely "nothing" is less than "something".

I know that the Fortran standards committee had to address the issue of character variable compares where the character strings are of unequal length andin light of the fact that Fortran performs a space fill.The standards committee chose to require that ('ABC' .eq. 'ABC '). I am not arguing against this decision. As it breaks less code. I am only stating that this may introduce a quirk when stringscontain characters less than space. If there is any chance of this quirk adversely addressing your program then it is recommended to write your own logical function to perform the compare on the strings in a manner suitable to your application .

Jim Dempsey

0 Kudos
Reply