[comp.lang.fortran] String manipulation.

quan@sol.surv.utas.oz (Stephen Quan) (10/24/90)

I need to do some string manipulation with a type : character*128.
Can I do the following?

       character*128 str1
       character str2(128)
       equivalence (str1,str2)

--
Stephen Quan
University of Tasmania

burley@world.std.com (James C Burley) (10/24/90)

In article <quan.656726015@sol> quan@sol.surv.utas.oz (Stephen Quan) writes:

   I need to do some string manipulation with a type : character*128.
   Can I do the following?

	  character*128 str1
	  character str2(128)
	  equivalence (str1,str2)

Yes, unless str1 or str2 are dummy arguments or some such thing.

However, I'm not sure what such an equivalence will buy you offhand.  For
example, given the above, any reference to STR2(N) should be identical
to a reference to STR1(N:N) (a substring reference).  If you have a FORTRAN 77
compiler, it should support substring references.  There are a few places,
however, where FORTRAN 77 does not support substring references but does
support array references -- for example, EQUIVALENCE (though I think Fortran
90 is extending EQUIVALENCE to support substrings) and the implied-DO form
of a DATA statement (though what you'd do with a substring reference within
this form within the constraints of the standard, I can't think of offhand!).
But in executable statements, I think you can always use substring references
wherever you can use array references, and you can even use both, as in
STR2(N)(1:1).  (In case you don't know, a substring reference, FOO(begin:end),
specifies a string <end-begin+1> characters long starting with the <begin>th
character of FOO, where the first character is numbered 1.  Omit begin, and
1 is the default; omit end, and the declared or assumed length of the variable
is the default (i.e. thru the end of the string).  One cannot omit the colon,
or it looks like an array element or function reference.  You can use
substring references on the left side of an assignment, in a READ statement,
and so on.)

James Craig Burley, Software Craftsperson    burley@world.std.com

hirchert@harriett.ncsa.uiuc.edu (Kurt Hirchert) (10/24/90)

In article <quan.656726015@sol> quan@sol.surv.utas.oz (Stephen Quan) writes:
>I need to do some string manipulation with a type : character*128.
>Can I do the following?
>
>       character*128 str1
>       character str2(128)
>       equivalence (str1,str2)

Yes, it is standard FORTRAN 77.  The only way you should encounter any
difficulty with a conforming compiler is if the maximum length of a character
string was set to something shorter than 128 characters.  I don't know of any
compiler with a limit that short, but the standard would permit it.
--
Kurt W. Hirchert     hirchert@ncsa.uiuc.edu
National Center for Supercomputing Applications

jerry@heyman.austin.ibm.com (Jerry Heyman) (10/25/90)

In article <quan.656726015@sol> quan@sol.surv.utas.oz (Stephen Quan) writes:
>I need to do some string manipulation with a type : character*128.
>Can I do the following?
>
>       character*128 str1
>       character str2(128)
>       equivalence (str1,str2)
>

Having written string manipulation functions in FORTRAN (MS FORTRAN v3.1-3.3)
I can say that the above will actually work with a single caveat.  In your
example, 'str1' CANNOT be a parameter.  If necessary, declare a second variable
to be a character*128 and do an assignment first.  FORTRAN does NOT allow
equivalences to parameters.

As mentioned, this worked on MS-FORTRAN, and I have also implemented the same
code on PRIMOS (rev 17.?) and on IBM mainframes using WATFIV.

>--
>Stephen Quan
>University of Tasmania

jerry heyman
-- 
Jerry Heyman                     IBM T-R: jerry@heyman.austin.ibm.com
AWD Tools Development            VNET   : HEYMAN at AUSVMQ
AWD Austin                       T/L    : 793-3962
*** All opinions expressed are exactly that - my opinions and NOT IBM's

andyo@glasperl.masscomp (Andy Oram) (10/25/90)

E-mail to Stephen Quan bounced, so I'm posting my two cents.

>        character*128 str1
>        character str2(128)
>        equivalence (str1,str2)

The EQUIVALENCE should work fine.  But before you code up a storm -- did you
check out standard FORTRAN facilities?  Character substrings, character
manipulation (LEN, INDEX, and other intrinsic functions), and internal files
(that is, reads and writes using character strings).  If you haven't, you
might be surprised how much you can do with them.

-------------------------------------------------------------------------------

Andrew Oram                            Concurrent Computer Corporation
(I don't represent Concurrent; this message represents my own opinion)

Digital Communications Route: andyo@westford.ccur.com
                              {harvard,uunet,petsd}!masscomp!andyo
Analog Communications Route:  (508) 392-2865

Automatic UN*X documentation utility:

          cref | compress | crypt | man

-------------------------------------------------------------------------------

GHAVERLA@vm.ucs.UAlberta.CA (G Haverland) (10/29/90)

>In article <quan.656726015@sol> quan@sol.surv.utas.oz (Stephen Quan) writes:
>>I need to do some string manipulation with a type : character*128.
>>Can I do the following?
>>
>>       character*128 str1
>>       character str2(128)
>>       equivalence (str1,str2)
>
 
I tried to do something similar a few years ago
with the Absoft FORTRAN compiler on the HP Integral
PC.  In one case (I believe the array of characters)
there was one character per word (32 bits), and the
other case there was 4 characters per word.  I was
examining the data space with the debugger when
I found this out.  I don't remember if I had equivalenced
the variables though.
 
Gordon Haverland, U of Alberta SLOWPOKE Facility