Detect empty string / variable?

Detect empty string / variable?

Greetings all. I want to perform some sort of logical test on a variable to determine if it's empty (or null value or the like). In MATLAB, it would be isempty(), but FORTRAN is not very forthcoming.

I'm very familiar with FORTRAN, but searches through help pdf-s haven't turned up anything I can use. Thanks in advance!
-Sailil

7 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

What exactly do you mean by "empty"? Fortran doesn't have the concept. The closest you can come is with a POINTER or ALLOCATABLE variable by asking if it is ASSOCIATED, but I can't figure out what "empty" would mean in general.

Steve

Retired 12/31/2016

Variables in Fortran only have a value, with the exception of NULL for Pointers, Allocated() for Allocatable arrays and LenTrim()==0 for Strings.

What you want to do is associate a property with other types of variables that convey specific information. There elegant methods, including defining more complex data types in Fortran 90, which will accomplish this.

However, the way I have done this forever is to identify ranges of the variable which are not valid for the problem at hand
eg., negative numbers for Temperature or negative values for "number of samples"

Define your own set of codes that occupy this "invalid" range and test for the presence of these values.

Of course, if the variables have no invalid range, you have a problem and associating logical variables may be the only solution.

Variables in Fortran only have a value, with the exception of NULL for Pointers, Allocated() for Allocatable arrays and LenTrim()==0 for Strings.

What you want to do is associate a property that conveys specific information with other types of variables. There are elegant methods, including defining more complex data types in Fortran 90, which will accomplish this. For example;

Type Scatter_Matrix
Logical (4) Initialized
Real (8) Cross_Section(:)
End Type

However, the way I have done this forever is to identify ranges of the variable which are not valid for the problem at hand
eg., negative numbers for Temperature or negative values for "number of samples"
Define your own set of codes that occupy this "invalid" range and test for the presence of these values.

If ( Cross Section(1) == -1.d30 ) then ! => not initialized
...
EndIf

Of course, they now caution you about using "==" with real numbers, so a better implementation might be

Logical (4) Test
Test = abs(Cross_Section(1) + 1.d30) < 1.d16
If ( Test ) then ! => not initialized
...
EndIf

If the variables have no invalid range, you have a problem and associating logical variables, possibly through defined type, may be the only solution.

As it happens, I am using strings in this case so I can use LEN_TRIM(), but I was hoping for a more robust detector. I'm implementing error checking in my subroutines, such that I want to make sure something is there before I start manipulating it. I want to know if a variable has a value (and equaling zero counts as having a value). In Matlab, I've made a habit of nargin and nargout-ing everywhere, along with isexist() and isempty(). If Fortran doesn't have such a creature, then I'll have to do without. For now, though, LEN_TRIM does what I'm looking for.

Thanks for all the help!!
Sailil

ps. To the forum administrator folk: Apparently this forum doesn't support the NOSPAM in emails. Therefore the "Sailil" profile needs trashed. Since I put NOSPAM in my email address, I didn't get mailed a password. And since I didn't get a password, I couldn't login as me. Also couldn't create a new profile because I still existed, I suppose. So if you can, get rid of "Sailil," I'll come back as my present "Saillil." Thanks again

Well, you can do something like:

IF (STRING == "") ...

This will test whether the string is all blank (remember that Fortran CHARACTER variables are fixed-length.)

Fortran does have a PRESENT intrinsic for use with OPTIONAL arguments (explicit interface is required when calling - see my Doctor Fortran article in the latest Visual Fortran newsletter (when the web server comes back up, that is!)

Steve

Retired 12/31/2016

"Sailil" has been deleted.

Steve

Retired 12/31/2016

Leave a Comment

Please sign in to add a comment. Not a member? Join today