Re: How to return a variable length substring from a function ?



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
.



Relevant Pages

  • Probability to find a substring in a string
    ... if every character can assume r different ... what's the probability to find a substring ... Let fbe the number of strings of length n that contain a string of ... yield anything. ...
    (sci.math)
  • Re: Unicode Support
    ... > Not knowing much about UTF-8 (my Unicode knowledge extends as far as ... > literal strings of this form as long as the character code for quote ... > can never appear in a MBCS (multibyte character sequence). ... then XP Notepad directly understands UNICODE and you can ...
    (alt.lang.asm)
  • Re: Need help on string manipulation
    ... better to convert strings to UCS-32 before manipulation? ... Characters represented by wchar_t must use one wchar_t per character, ... which may use a multibyte encoding. ... use some newer Unicode characters, if this is a problem for you, then ...
    (comp.lang.c)
  • Re: Copying string to byte array
    ... of Strings and the CryptEncrypt + CryptDecrypt APIs. ... binary data should not be held in String variables. ... a) not all character codes are valid in a given ...
    (microsoft.public.vb.general.discussion)
  • Re: Zero terminated strings
    ... standard library that told you how to encode strings.) ... this redundant terminator as well). ... (Other string libraries like Vstr have ...
    (comp.lang.c)