Re: How to return a variable length substring from a function ?
- From: Dave Allured <nospom@xxxxxxxxxx>
- Date: Wed, 28 Jan 2009 11:33:37 -0700
Ragu wrote:
Hello,
I have a function that returns a variable length character substring.
Depending on the input, the output is either 1 to 3 characters long.
How can I return a character(len = 1) or character (len = 2) or
character (len = 3) result depending on the input?
I can not seem to figure out how after looking in fortran books.
In classic Fortran (77/90/95 etc), a user function can return only a
fixed lenth character string. However, you can always do the next best
thing which is to emulate variable length.
This is one example of the general issue of emulating variable length
strings within fixed length strings. It is a false myth that Fortran
can't handle variable length strings. Though not perfect, this is quick
and easy once you get used to a handful of constructs. Some of the
textbooks I have seen do not cover this very well.
Most commonly used for this purpose are intrinsic functions trim,
len_trim (and predecessor lnblnk), adjustl, adjustr; deferred length
subroutine inputs (len=*); the 'a' format specifier with no length
field; and substring indexing (str(m:n)).
The most common convention is to keep strings left justified within
fixed length character*(n) variables. Declare variables longer than
needed, for insurance. Then, strip off the blanks with trim() as needed
within the application, and otherwise forget that the blanks are there.
I have a lot of code like this, e.g.:
filename = trim (base) // '_' // trim (numstr) // '.' // ext
Note that you can always omit trim() on the *last* operand in
concatenates for assignment, because of Fortran right hand padding
rules.
When leading blanks are possible as well as trailing, use
trim(adjustl(str)). Preserving leading or trailing blanks requires more
explicit programming, depending on the exact needs of the application.
In rare cases I have used substring indexing and actually kept track of
the start and end positions of the substring of interest. "There is
more than one way to do this." ;-)
Your sample function appears to make nice left justified output strings,
so that is okay as written. The only thing I might change is the
*usage* of the function result strings in the calling program:
write(*,'("1 = ", A)') trim (Ex_columnletter(1))
write(*,'("16384 = ", A)') trim (Ex_columnletter(16384))
Rather than what you had:
write(*,'("1 = ", A4)') Ex_columnletter(1)
write(*,'("16384 = ", A4)') Ex_columnletter(16384)
Well this was a bit of a rant, but I hope you find it useful.
--Dave
.
- Follow-Ups:
- Re: How to return a variable length substring from a function ?
- From: Richard Maine
- Re: How to return a variable length substring from a function ?
- References:
- Prev by Date: Re: fortran equivalent to C
- Next by Date: Re: Inheritance of abstract types: accessing the parent type (F2003 standard question)
- Previous by thread: Re: How to return a variable length substring from a function ?
- Next by thread: Re: How to return a variable length substring from a function ?
- Index(es):
Relevant Pages
|