- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I was searching the forum for threads on assumed-length CHARACTER functions and found a link to an e-newsletter with the following:
"Assumed-length CHARACTER functions (and the CHARACTER* form of declaration) are deemed to be an "irregularity" in the language, which they are, and there are alternatives available, but Dr. Fortran doesn't see these disappearing from users' code anytime soon."
Can anyone enlighten me on the proper Fortran 95 alternatives? I want a function that returns an integer as a character string with no leading or trailing blanks.
Thanks,
Andy
"Assumed-length CHARACTER functions (and the CHARACTER* form of declaration) are deemed to be an "irregularity" in the language, which they are, and there are alternatives available, but Dr. Fortran doesn't see these disappearing from users' code anytime soon."
Can anyone enlighten me on the proper Fortran 95 alternatives? I want a function that returns an integer as a character string with no leading or trailing blanks.
Thanks,
Andy
Link Copied
10 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
The character(*) result form will not do what you want. Here is some code that will work for and KIND of integer, although unfortunately it doesn't work in the current version of CVF. Hopefully it will work in the next release. BTW Steve, there were a couple of bugs encountered in some previous threads that weren't explicitly acknowledged as such in this forum (renaming MATMUL and improper arguments to the ASSOCIATED intrinsic.) Have these been fixed in the next version of CVF or should I send bug reports?
! File: int2string.fi pure function int2string_len(i) integer int2string_len integer(template_kind), intent(in) :: i character(20) string write(string,'(i0)') i int2string_len = len_trim(adjustl(string)) end function int2string_len function int2string(i) integer(template_kind), intent(in) :: i character(int2string_len(i)) int2string write(int2string,'(i0)') i end function int2string ! End of file: int2string.fi ! File: int2string.f90 module mykinds implicit none integer, parameter :: ik1 = selected_int_kind(2) integer, parameter :: ik2 = selected_int_kind(4) integer, parameter :: ik4 = selected_int_kind(9) integer, parameter :: ik8 = selected_int_kind(18) end module mykinds module ik1_mod use mykinds, only: template_kind => ik1 implicit none private public int2string contains include 'int2string.fi' end module ik1_mod module ik2_mod use mykinds, only: template_kind => ik2 implicit none private public int2string contains include 'int2string.fi' end module ik2_mod module ik4_mod use mykinds, only: template_kind => ik4 implicit none private public int2string contains include 'int2string.fi' end module ik4_mod module ik8_mod use mykinds, only: template_kind => ik8 implicit none private public int2string contains include 'int2string.fi' end module ik8_mod module generic_recombination use ik1_mod, only: int2string_ik1 => int2string use ik2_mod, only: int2string_ik2 => int2string use ik4_mod, only: int2string_ik4 => int2string use ik8_mod, only: int2string_ik8 => int2string implicit none interface int2string module procedure int2string_ik1, int2string_ik2, & int2string_ik4, int2string_ik8 end interface int2string end module generic_recombination program int2string_prog use generic_recombination use mykinds implicit none write(*,'(a)') ' #'//int2string(-1_ik1)//'#' write(*,'(a)') ' #'//int2string(-1_ik2)//'#' write(*,'(a)') ' #'//int2string(-1_ik4)//'#' write(*,'(a)') ' #'//int2string(-1_ik8)//'#' write(*,'(a)') ' #'//int2string(huge(1_ik1))//'#' write(*,'(a)') ' #'//int2string(huge(1_ik2))//'#' write(*,'(a)') ' #'//int2string(huge(1_ik4))//'#' write(*,'(a)') ' #'//int2string(huge(1_ik8))//'#' end program int2string_prog ! End of file: int2string.f90 Output (on a compiler where this works) #-1# #-1# #-1# #-1# #127# #32767# #2147483647# #9223372036854775807#
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
The 'i0' edit descriptor isn't standard, is it?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
The 'i0' edit descriptor is standard f95. CVF supports it since v. 6.0. It's kind of difficult to find this in Compaq's documentation, though. I can see it under Index | Edit descriptors | I and then selecting 'General rules for numeric editing' at the bottom of the page, and then the behavior is in green near the bottom... probably this feature could have been more prominently documented, since I use 'i0' more than any other form of integer edit descriptor.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
BTW Steve, there were a couple of bugs encountered in some previous threads that weren't explicitly acknowledged as such in this forum (renaming MATMUL and improper arguments to the ASSOCIATED intrinsic.) Have these been fixed in the next version of CVF or should I send bug reports?
You should always send bug reports. I don't recall the specific issues you're referring to from discussions in the forum, so it's best to send specific examples to vf-support@compaq.com. I may not catch everything written in the forum.
Steve
You should always send bug reports. I don't recall the specific issues you're referring to from discussions in the forum, so it's best to send specific examples to vf-support@compaq.com. I may not catch everything written in the forum.
Steve
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Andy,
Here is a somewhat simpler solution depending upon your needs:
The downside is that you have to access STRING with array subscripts from 1 to L everywhere you want to use this in the code.
Tom
You should easily be able to turn this into a function or a subroutine. The only downside is that you have to reference each string with (1:L)
Here is a somewhat simpler solution depending upon your needs:
CHARACTER(20) :: STRING WRITE (STRING,'(A)') ' 123456789 ' STRING = ADJUSTL(STRING) L = LEN_TRIM(STRING) WRITE (*,'(A)') '#'//STRING(1:L)//'#'
The downside is that you have to access STRING with array subscripts from 1 to L everywhere you want to use this in the code.
Tom
You should easily be able to turn this into a function or a subroutine. The only downside is that you have to reference each string with (1:L)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Andy,
Here is a somewhat simpler solution depending upon your needs:
The downside is that you have to reference STRING throughout the rest of the code using the (1:L) substring in order to eliminate the trailing blanks.
Tom
Here is a somewhat simpler solution depending upon your needs:
CHARACTER(30) :: STRING WRITE (STRING,'(A)') ' 123456789 ' STRING = ADJUSTL(STRING) L = LEN_TRIM(STRING) WRITE (*,'(A)') '#'//STRING(1:L)//'#' OUTPUT: #123456789#
The downside is that you have to reference STRING throughout the rest of the code using the (1:L) substring in order to eliminate the trailing blanks.
Tom
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Sorry about the double message - sometimes this forum leaves a lot to be desired, but it is great to have anything that can be used as an additional resource.
Tom
Tom
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Sigh...why is it so difficult to get 5 lines of code correct? Here is what I should have posted
Tom
CHARACTER(30) :: STRING I = 123456789 WRITE (STRING,'(I0)') I L = LEN_TRIM(STRING) WRITE (*,'(A)') STRING(1:L)
Tom
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thanks to James Van Buskirk for the program - it does work OK in CVF6.6 except that CVFseems to have a problem in generating HUGE for Integer*8 in some, but not all places. If one repllaces "huge(1_ik8)" in the last print line with "n" (without the inverted commas) where n is Integer*8 and is set to 2**63-1, you will get JVB's complete output.
Bear of little brain
Bear of little brain
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Whoops. I assumed that this wouldn't compile due to other bugs (15493 and 15596) so I didn't actually try compiling it with CVF. Neither of them have any effect here. The bug that affects this code is new to me (16161.)

Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page