0

I have a character array names(10)*6. I'm looping through I = 1:10 and writing the values of names(I).

The problem is that names periodically is missing values, and if thats the case for a particular names(I), I want to skip it.

I was trying to do something like this.

   IF(names(I) .NE. 0) THEN 
        WRITE(4,202) names(I)   
    ENDIF

I got an error telling me I'm dumb for comparing characters to 0. That makes sense. What should I compare it to? Empty spaces like this? How do I check if its not defined or empty after I declared space for it?

IF(mychar(I) .NE. '      ') THEN 
     WRITE(4,202) names(I)  
ENDIF

The goal is to only issue the write command if there is actually something there. :-)

Edit Note: I may not initialize this array. I am wondering what the default value for an undefined declared index is, or if there is a function to check if a character array index is empty.

4
  • Why not initialize it to an empty string prior to usage? Then, you can do the check you proposed... Commented Jun 25, 2014 at 14:43
  • 2
    Describe the situation in more detail in the question. Show what you really want to achieve, not just what you mistakenly think is the right way how to do it. Commented Jun 25, 2014 at 15:37
  • 1
    fortran strings are blank padded, so LEN_TRIM(string).eq.0 , string.eq.'' , and string.eq.' ' are all equivalent. ( And all will likely be false if the string was never initialised. ) Commented Jun 25, 2014 at 18:54
  • Response to the "Edit note". By "undefined" I assume that you mean "uninitialized". Fortran provides no default values for uninitialized variables. You should initialize the variable before comparing it. However, when you assign to a string with a shorter string, the remaining trailing characters are filled with blanks. Perhaps that it what you are asking. Commented Jun 26, 2014 at 2:43

1 Answer 1

1

You might be interested in the intrinsic len_trim function. As always Read The Fortran Manual.

Sign up to request clarification or add additional context in comments.

1 Comment

I think this will be an effective way to check if it has been defined, I will try it out. Thank you for the response.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.