Getting a variable format specification, and character strings 
Author Message
 Getting a variable format specification, and character strings

Quote:

> Hello,

> Is it possible in f77 to have a format statement that can
> have a variable in place of the constant specification ...
> e.g. in a subroutine that converts integers to character strings

>      SUBROUTINE i2a(number, label)

>      INTEGER number
>      CHARACTER*(*) label

>      write( label, '(i4.4)') number

>      RETURN
>      END

> here the 4 restricts length of 'label' to 4, instead of having
> a multitude of write statement for every conceivable size of
> label (using len to find the size of label) how can i write
> the format statement to use a variable ix.x ?

A format specifier cannot "contain" a variable.  It can, however, _be_
a (character) variable.  At some point, however, you will need to have
the length of the label, as you would even if there were a way to
directly incorporate a variable field width into a format specifier.
You seem to have sufficient command of the string operations to figure
out how to construct an appropriate format specifier in a variable,
given the field length you want to use as a parameter.

An alternative approach which may be simpler would be to just write the
number to a sufficiently large buffer and then write code to do the
zero-padding manually, depending on the length required.

John Bollinger



Mon, 31 Jan 2000 03:00:00 GMT  
 Getting a variable format specification, and character strings

Hello,

Is it possible in f77 to have a format statement that can
have a variable in place of the constant specification ...
e.g. in a subroutine that converts integers to character strings

     SUBROUTINE i2a(number, label)

     INTEGER number
     CHARACTER*(*) label

     write( label, '(i4.4)') number

     RETURN
     END

here the 4 restricts length of 'label' to 4, instead of having
a multitude of write statement for every conceivable size of
label (using len to find the size of label) how can i write
the format statement to use a variable ix.x ?

on the subject of character strings. how does a character string
look in memory? is there a null character to mark the end?
(because len() seems to return the full length of it, rather
than what's actually been written into). how do strings differ
in C as compared to fortran (memory-wise)? i have assumed
that a fortran string is just a byte-sized version of the
integer array.

Are there any archives on the internet with utility subroutines
and functions that are free to use and deals with things like the
above?

thanks



Mon, 31 Jan 2000 03:00:00 GMT  
 Getting a variable format specification, and character strings

Quote:

>Is it possible in f77 to have a format statement that can
>have a variable in place of the constant specification ...
>e.g. in a subroutine that converts integers to character strings

>     SUBROUTINE i2a(number, label)

>     INTEGER number
>     CHARACTER*(*) label

>     write( label, '(i4.4)') number

>     RETURN
>     END

>here the 4 restricts length of 'label' to 4, instead of having
>a multitude of write statement for every conceivable size of
>label (using len to find the size of label) how can i write
>the format statement to use a variable ix.x ?

The following does something like what you want:

      SUBROUTINE i2a(number, label)

      INTEGER number
      CHARACTER*(*) label
      CHARACTER*8 form

      l=nd(number)
      write( form, '(a,i1,a,i1,a)') '(i',l,'.',l,')'
      write( label, form) number

      RETURN
      END
      function nd(k)
c
c  Returns the number of digits in integer K; adds one for minus sign if
c  K is negative; K=0 returns 1
c
      nd=1
      l=abs(k)
      dowhile (l.ge.10)
         nd=nd+1
         l=l/10
      enddo
      if (k.lt.0) nd=nd+1
      return
      end

Andy Mai



Mon, 31 Jan 2000 03:00:00 GMT  
 Getting a variable format specification, and character strings

...

Quote:
>      write( label, '(i4.4)') number
...
> here the 4 restricts length of 'label' to 4, instead of having
> a multitude of write statement for every conceivable size of
> label (using len to find the size of label) how can i write
> the format statement to use a variable ix.x ?

One non-standard syntax supported by some of DEC's compilers is:

        write ( label, '(i<X>.<X>)' ) number

Where X is the name of a variable in your program.

You can get a similar effect by using an internal write into a
a format string and then doing the write you really care about:

        write ( format-string, '( ''(i'', i2.2, ''.'', i2.2, '')'' )' ) x, x
        write ( label, format-string ) number

(Tested and hopefully transcribed correctly.  :-)

Quote:
> on the subject of character strings. how does a character string
> look in memory? is there a null character to mark the end?

No, there is no null character.  You can put one there if you want.

Quote:
> (because len() seems to return the full length of it, rather
> than what's actually been written into).

Right.  Fortran-77 strings are fixed length.

Quote:
> in C as compared to fortran (memory-wise)? i have assumed
> that a fortran string is just a byte-sized version of the
> integer array.

Yep.  That's how I visualize them.  I don't think the language standard
specifies a memory layout though.  On some architectures the layout
might be more interesting than others.

With the compilers I use, a Fortran string can be passed as an argument
to a C subroutine as long as you manually insert the null terminator that
the C subroutine may be expecting and as long as you specify the
correct parameter passing mechanism.

        FORTRAN_STRING(LAST_CHAR:LAST_CHAR) = CHAR(0)
        CALL C_SUBROUTINE ( %REF(FORTRAN_STRING) )




Mon, 31 Jan 2000 03:00:00 GMT  
 Getting a variable format specification, and character strings


        >Hello,

        >Is it possible in f77 to have a format statement that can
        >have a variable in place of the constant specification ...
        >e.g. in a subroutine that converts integers to character strings

        >     SUBROUTINE i2a(number, label)

        >     INTEGER number
        >     CHARACTER*(*) label

        >     write( label, '(i4.4)') number

        >     RETURN
        >     END

You need to create the length/width specification
dynamically.  To do this, you need to do an
internal WRITE, and specify the format as a CHARACTER
variable.

        >on the subject of character strings. how does a character string
        >look in memory? is there a null character to mark the end?

No.  The length specification in the declaration states
how long the string will be.

        >(because len() seems to return the full length of it, rather
        >than what's actually been written into)

The declared length is always the length.  In other words
the implementation is fixed-length strings.  Varying-length
strings are not implemented.

        > how do strings differ
        >in C as compared to fortran (memory-wise)? i have assumed
        >that a fortran string is just a byte-sized version of the
        >integer array.

        >Are there any archives on the internet with utility subroutines
        >and functions that are free to use and deals with things like the
        >above?

There's a varying-length string package for Fortran 90,
available thru the www..

        >thanks



Tue, 01 Feb 2000 03:00:00 GMT  
 Getting a variable format specification, and character strings

Formats can be character strings so you can write the format using the
variable before you use it.
 character ch40*40
 i=30
 write(ch40,'(i2,a)')i,'f3.2'
 write(label,ch40)...........


Quote:
> Hello,

> Is it possible in f77 to have a format statement that can
> have a variable in place of the constant specification ...
> e.g. in a subroutine that converts integers to character strings

....


Wed, 02 Feb 2000 03:00:00 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. variable FORMAT specification ?

2. is there a variable format specification?

3. Getting characters from string, maybe with preg_grep (Newbie question)

4. Getting Rid of 1st and last character in a string

5. String formatting characters - looking for details

6. Format statements via character variables

7. Bug in character string hex formatting

8. add a0 format to language to print trimmed character variables

9. Invalid format character in string

10. Beginner: Character from a string, variable types

11. Assigning a number string to a character variable

12. Reading variable length character strings

 

 
Powered by phpBB® Forum Software