views:

129

answers:

1

Hello Group,

I have to fix a bug in a very old and large financial system that uses Fortran, C and C++. I am primarily a C++ guy, have no idea abt Fortran! I have a problem understanding a Fortran statement which i think has caused a nasty bug in our systems....

if (instructions .lt. ' ')  then
    instructions = ' '
endif

instructions is a text/string.

How does the above code behave, does it compare only the first character (atleast my tests suggest)? Basically this is a production issue, I am trying to give a workaround to my clients. A correct Fortran code to compare strings filed would also do...

Thanks in advance

+2  A: 
 if (instructions .lt. ' ')  then 

.lt. is Fortram means "less than", so, if instructions is "less than" a space, replace it with a space. It will consider the entire string, but since the right-hand-side is just a single space, only needs to look at the first character of instructions; If the first character is less than a space (i.e., it's a control-char, if we are talking about ASCII), than it's less than; if the first chanracter is a space or greater (i.e. a printable character), than it's not less-than.

James Curran
One possibly subtle aspect: if the declared length of the string "instructions" is longer than one, the assignment statement (if invoked) will set the remainder of the string "instructions" to spaces. Fortran fixed-length strings get padded on the right with spaces.
M. S. B.