nullfortrantrimnonblank

Why does trim(" ") == "" returns false in Fortran?


I'm facing a random incoherence... In some cases, trim(" ") == "" returns false.

I did something like this (so a sort of concatenation function using types) of course this code is a bit messy a lot of simplification can be done but because of this trim problem I did it like this to be sure that nothing is messing :

character*150 :: tab(5)
character*150 :: var
character*50 :: a
character*50 :: b
character*50 :: c
character*50 :: d
a= '' ! comes from a type nameType%pref
b= '' ! parameter
c= '' ! comes from a type nameType%suf
d= 'deviceName' ! parameter
if (trim(d) .ne. '') then
    var = d
else
    if (trim(c) .ne. ''  .and. (trim(a) .ne. '' .or. trim(b) .ne. ''))then
            var = trim(a)//trim(c)//trim(b)
    else
        var = ''
    end if
end if
var = trim(a)//trim(b)//trim(c)
tab(1) = var
if (trim(tab(1)) .eq. '') then
     print*, ("hi")
end if

Here is the exit at the end of this function :

trim(var) :
len_trim(var) :            0 
trim(var) == " : T

This code is sometimes working and sometimes not... (I mean I have other empty variables and I'm facing this problem only with some variables) I think the space character is encrypted because in debug mode I have random values like "pÏ"...

Here is the exit at the error printing and when I ask for the ASCII value :

    trim(var) : 
    len_trim(var) :          150
    trim(var) == " : F
    ichar(var(j:j)), ">"//var(j:j)//"<"
           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >           0 >
    

So trim(tab(i)) is doing a trim of 150 NULL.

I created this function to solve that problem :

      logical function isNULL(var)
          implicit none
          Character *(*) :: var
          logical :: ret
          ret = ichar(var(1:1)) .eq. 0
          isNULL = ret
          return 
      end function

Solution

  • The function TRIM does:

    7.170 TRIM (STRING)
    Description: String without trailing blanks.
    Class: Transformational function.
    Argument: STRING shall be a character scalar.
    Result Characteristics: Character with the same kind type parameter value as STRING and with a length that is the length of STRING less the number of trailing blanks in STRING. If STRING contains no nonblank characters, the result has zero length.
    Result Value: The value of the result is the same as STRING except any trailing blanks are removed.
    Example: TRIM (" A B ") has the value " A B".

    source: Fortran 2008 Standard

    As you notice, the word blank, is appearing a lot in this definition and represents a white space in the respective character set.

    My suspicion is that you are processing data coming from a DOS-box which has CRLF line termination. Or any other unprintable character at the end of your string. You could verify this by printing each character of the string and it's corresponding ASCII number.

    Eg if your string is str:

       do i=1,len(str)
          print *, ichar(str(i:i)), ">"//str(i:i)//"<"
       done
    

    whitespace characters should have value 32. If you have a different value, you can have a look here: http://www.asciitable.com/