Path: utzoo!utgpu!water!watmath!clyde!cbosgd!ihnp4!homxb!mtuxo!mtune!icus!gil From: gil@icus.UUCP (Gil Kloepfer Jr.) Newsgroups: comp.os.vms Subject: Re: FORTRAN singularity Summary: FORTRAN strings really have no "length" Message-ID: <256@icus.UUCP> Date: 8 Feb 88 04:43:53 GMT References: <8802061416.AA15860@ucbvax.Berkeley.EDU> Reply-To: gil@icus.UUCP (Gil Kloepfer Jr.) Organization: Bowne Management Systems, Inc., Mineola, NY Lines: 69 In article <8802061416.AA15860@ucbvax.Berkeley.EDU> CURLEY@WHARTON.UPENN.EDU ("Curley, Robert F.") writes: > >I tripped over the following this week (VMS 4.6, FORTRAN 4.71-271) >------------------------------------------------------------------------ >$TYPE TEST.FOR > CHARACTER*8 RED > RED = '' > END > >$ FORTRAN TEST >%FORT-E-ZERLENSTR, Zero-length string > [RED = ''] in module TEST$MAIN at line 2 >%FORT-F-MISSDEL, Missing operator or delimiter symbol > [RED = ''] in module TEST$MAIN at line 2 >%FORT-F-ENDNOOBJ, DRA7:[CURLEY.WORKING]TEST.FOR;1 completed with 2 diagnostics- > object deleted >----------------------------------------------------------------------- >Colorado TSC said that there are no zero length strings permitted in >FORTRAN. This is correct. The reason for this is because FORTRAN only has fixed-length strings. The fact that you assign a shorter string constant to a character variable is really a convenience and not a language feature. In effect, FORTRAN pads the string in any case with blanks, so for the example above, all the following statements are equivalent: RED=' ' RED=' ' RED=' ' RED=' ' (etc. You get the idea...) If you want to give the string a "length" so to speak, the way I have been doing it is to use a group of subroutines which looks back from the end of the character variable until it finds a non-blank character or the beginning of the string. It then returns that position in the string, and that is the "length", or usable length of the string. As I did, you can write a bunch of C-like subroutines for string manipulation. Note also (important!) that the VAX FORTRAN LEN() function returns the _declared_ length of the string. This means that no matter what you assigned to RED in the example above, the number returned by LEN would be 5. This is a very important concept to get straight since many programmers get frustrated by character strings, and begin using arrays of "BYTE"'s as character strings. This is OK, except that moving BYTE arrays is inefficient when compared to the simple assignment function of character variables (I'm not sure on this, but it would seem logical that the developers of the VAX FORTRAN compiler took advantage of the string move INSTRUCTION of the VAX when handling string moves, etc). If asked what my preference was: use BYTE arrays of CHARACTER variables for character strings, I would choose the latter without a thought. Hope this helps many. FORTRAN was not originally designed to handle character strings at all, and thus many people tend to doubt that the more recent versions of FORTRAN and some extended FORTRANs can actually handle character manipulation well, if at all. It does, indeed, function well when you understand its merits and limitations. +====================================+========================================+ | Gil Kloepfer, Jr. | Net-Address: | | Senior Programmer | {boulder,ihnp4,talcott}!icus!gil | | Bowne Management Systems, Inc. | Voice: Home: (516) 968-6860 | | 235 E. Jericho Turnpike | Office: (516) 248-6840 x796 | | Mineola, New York 11501 | Internet: gil@icus.UUCP | +====================================+========================================+ | Disclaimer: My employers know I'm right, but will never admit to it... | +=============================================================================+