- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page