NEED HELP appending two Char variables of variable length 
Author Message
 NEED HELP appending two Char variables of variable length

Hello ,
        I am trying append a charater string to a character variable (variable length of 1-25 characters) using '//' as shown below.  I had defined my charater
variable length as 25 and when the variable (as input by the user) is less than
25 character, its leaving some blanks (by the diff of 25 and the actual length
of the variable as input) between the variable and the string.  I want to put
the string just after the charater variable as input by the user, NOT ANY BLANKS IN BETWEEN.  An e-mail reply will be appreciated.  Thanks in advance

        program such-n-such
        character meshname*25,stringf*29
c
        portion not included
c
        ...
        ..
        ..
c
        read(iunit,108) meshname
 108    format(1a25)
        stringf = meshname // ".msh"
        write(*,*)stringf
c
------------------------------------------
        When I give an input for meshname as (newmesh) its creating the
        variable stringf as (newmesh                  .msh) NOT as "newmesh.msh"      the way I want..  Can someone show me how to do this?



Tue, 12 Dec 1995 01:26:01 GMT  
 NEED HELP appending two Char variables of variable length

Quote:
>Hello ,
>    I am trying append a charater string to a character variable (variable length of 1-25 characters) using '//' as shown below.  I had defined my charater
>variable length as 25 and when the variable (as input by the user) is less than
>25 character, its leaving some blanks (by the diff of 25 and the actual length
>of the variable as input) between the variable and the string.  I want to put
>the string just after the charater variable as input by the user, NOT ANY BLANKS IN BETWEEN.  An e-mail reply will be appreciated.  Thanks in advance

      The function below will do what you wanted. It does not
      remove the leading blanks, however.

Mike Shen                                 Computational Physics Inc.

BTW, this is not my account, I am simply using it with permission.

C
C
C********************************************************************
      FUNCTION CONCAT(ASTR,BSTR)
C********************************************************************
C     Purpose:
C        This routine concatenates two strings (the trailing blanks
C        of the first string are ignored).
C
C     Arguments:
C        (in) astr = first string.
C        (in) bstr = second string.
C////////////////////////////////////////////////////////////////////
      IMPLICIT NONE
C
      INTEGER IPOS
      CHARACTER*(*) CONCAT, ASTR, BSTR
C
      IPOS = INDEX(ASTR,' ')
C
      IF (IPOS .NE. 0) THEN
         CONCAT = ASTR(1:IPOS-1)//BSTR
      ELSE
         CONCAT = ASTR//BSTR
      ENDIF
C
      RETURN
      END



Tue, 12 Dec 1995 04:54:39 GMT  
 NEED HELP appending two Char variables of variable length

Moitra) says:

Quote:

>Hello ,
>        I am trying append a charater string to a character variable...
>...                                                                           r
>...I want to put
>the string just after the charater variable as input by the user, NOT ANY     S
>BLANK IN BETWEEN. ...
>...
>        read(iunit,108) meshname
> 108    format(1a25)
>        stringf = meshname // ".msh"

If you happen to be using fortran 90, nothting could be easier -- just
use the TRIM intrinsic function to trim the trailing blanks from
meshname:

        stringf = TRIM(meshname) // ".msh"

In Fortran 77 a general solution (assuming meshname could contain
leading or embedded blanks, or could be entirely blanks) would look
something like this (I have changed some variable names to conform
to the Fortran 77 6-character limit):

      DO 10 LAST = LENGTH(MSHNAM),1,-1
         IF (MSHNAM(LAST:LAST).NE.' ') GOTO 11
   10 CONTINUE
   11 IF (LAST.EQ.0) THEN
         STRNGF = '.msh'
      ELSE
         STRNGF = MSHNAM(1:LAST) // '.msh'
      ENDIF

--

Stanford Linear Accelerator Center, MS 97 | reflect those of SLAC,
P.O. Box 4349; Stanford, CA  94309        | Stanford or the DOE



Mon, 11 Dec 1995 23:13:01 GMT  
 NEED HELP appending two Char variables of variable length

Quote:
>In Fortran 77 a general solution (assuming meshname could contain
>leading or embedded blanks, or could be entirely blanks) would look
>something like this (I have changed some variable names to conform
>to the Fortran 77 6-character limit):

>      DO 10 LAST = LENGTH(MSHNAM),1,-1
>         IF (MSHNAM(LAST:LAST).NE.' ') GOTO 11
>   10 CONTINUE
>   11 IF (LAST.EQ.0) THEN
>         STRNGF = '.msh'
>      ELSE
>         STRNGF = MSHNAM(1:LAST) // '.msh'
>      ENDIF

I suppose a lot of this comes down to a question of programming style.
I prefer to do things like this by using the following integer
function:

      INTEGER FUNCTION RIGHT (A)
      CHARACTER* (*) A
C
C     This function returns the position of the
C     rightmost non-space character in the input
C     character string A.
C
      DO 1 RIGHT = LEN (A), 1, -1
        IF (A (RIGHT : RIGHT) .NE. ' ') RETURN
1     CONTINUE
C
C     failure: if we just return now the return value
C     will be zero.  It is our callers responsibility
C     to detect and handle such cases.
C
      RETURN
      END

and then writing:

      A (RIGHT (A) + 1:) = '.msh'

to append the characters '.msh' to the character string in A.

If you wanted to make your code a little more bullet proof, you
could elaborate this to:

      IF (RIGHT (A) .NE. LEN (A)) A (RIGHT (A) + 1:) = '.msh'

Little functions like RIGHT, and it's obvious companion LEFT, can
be quite useful if you have a lot of character manipulation to do.
--
Dennis Davis, BUCS, University of Bath, Bath, BA2 7AY, UK



Tue, 12 Dec 1995 19:41:17 GMT  
 NEED HELP appending two Char variables of variable length

[deletia]

Quote:
> 108        format(1a25)

A point about programming style, the following is less likely to cause
problems/misunderstandings and (IMHO) substantially cleaner:

        108  format(a)

The leading 1 is superfluous, and the trailing 25 may conflict with the defined
size of the variable.  For output, I have seen some _bizarre_ results when
`aw' is used in a format and w .ne. len().  To limit the length of a string on output,
use a character substring on the write, _not_ a width in the format.
--
---------------------------MY_COMMENTS, MY_OPINIONS---------------------------
David N. Lombard    The first thing we do,         The MacNeal-Schwendler Corp

(213) 259-4911      II Henry VI, IV.ii             Los Angeles, CA  90041



Wed, 13 Dec 1995 04:19:32 GMT  
 NEED HELP appending two Char variables of variable length

Quote:


> >In Fortran 77 a general solution (assuming meshname could contain
> >leading or embedded blanks, or could be entirely blanks) would look
> >something like this (I have changed some variable names to conform
> >to the Fortran 77 6-character limit):

> >      DO 10 LAST = LENGTH(MSHNAM),1,-1
> >         IF (MSHNAM(LAST:LAST).NE.' ') GOTO 11
> >   10 CONTINUE
> >   11 IF (LAST.EQ.0) THEN
> >         STRNGF = '.msh'
> >      ELSE
> >         STRNGF = MSHNAM(1:LAST) // '.msh'
> >      ENDIF

> I suppose a lot of this comes down to a question of programming style.
> I prefer to do things like this by using the following integer
> function:

>       INTEGER FUNCTION RIGHT (A)
>       CHARACTER* (*) A
> C
> C     This function returns the position of the
> C     rightmost non-space character in the input
> C     character string A.
> C
>       DO 1 RIGHT = LEN (A), 1, -1
>         IF (A (RIGHT : RIGHT) .NE. ' ') RETURN
> 1     CONTINUE
> C
> C     failure: if we just return now the return value
> C     will be zero.  It is our callers responsibility
> C     to detect and handle such cases.
> C
>       RETURN
>       END

> and then writing:

>       A (RIGHT (A) + 1:) = '.msh'

> to append the characters '.msh' to the character string in A.

> If you wanted to make your code a little more bullet proof, you
> could elaborate this to:

>       IF (RIGHT (A) .NE. LEN (A)) A (RIGHT (A) + 1:) = '.msh'

> Little functions like RIGHT, and it's obvious companion LEFT, can
> be quite useful if you have a lot of character manipulation to do.
> --
> Dennis Davis, BUCS, University of Bath, Bath, BA2 7AY, UK


Is this really so hard?  My solution was the following...
     loc1 = index(mshname, '   ')  !Assume at least 3 blanks at the end of mshname
                                                       !Or make mshname a50 to ensure this
                                                       !Can also increase number of blank spaces if necessary.
     loc1 = loc1 - 1
     strngf = mshname(1:loc1)//".msh"

Can also add logic to make loc1 equal to length of string if no triple space encountered.
Is the use of index non-standard?  
I don't see why all the above work would be necessary.

--

 ))))/   Andy Loughe                          
  , ,    Goddard Space Flight Center      
   J     Code 971  Bldg. 22  Room 380-B        phone : (301) 286-3995

        __/__/           __/         __/        __/__/__/         __/       __/
    __/      __/        __/__/      __/        __/      __/        __/    __/
   __/      __/        __/ __/     __/        __/       __/         __/ __/
  __/__/__/__/        __/    __/  __/        __/       __/           __/
 __/      __/        __/      __/__/        __/       __/           __/
__/      __/        __/         __/        __/__/ __/              __/



Sun, 17 Dec 1995 00:51:09 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Char variable of variable length

2. appending integer variables to char strings

3. Clipper programmer needs help - Have outgrown Clipper 5x variable length

4. Update to message on strings of variable length using a function which expands to 25 char

5. Processing variable length/variable data files

6. Variable length variable

7. concatenate two variables in a variable

8. Getting true length of a variable length record - IBM Mainframe

9. Finding Variable-Length Record Length

10. Help Variable Length Output Records

11. Help: Variable Length Records--Special Case

12. variable length fields - help?!

 

 
Powered by phpBB® Forum Software