- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
If I build and run the attached program I get
Method 1:
{Monday }
{Tuesday }
{Wednesday}
{Thursday }
{Friday }
{Saturday }
{Sunday }
but I don't understand why. I'd have thought that it should be
Method 1:
{Monday}
{Tuesday}
{Wednesday}
{Thursday}
{Friday}
{Saturday}
{Sunday}
which I can get by using trim(n) on line 22. Could someone please explain to me why I get the first result. Thanks.
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
When you have:
week = ['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday']
this is an array constructor with elements of different lengths. If you compile with standards checking, you'll get:
If type specification is omitted, each ac-value expression in the array constructor of type CHARACTER must have the same length type parameters.
What we do as an extension is pad the shorter elements with blanks to the length of the longest element. So you're assigning values all the same length to the allocatable character components.You need the trim. To be standard compliant, you also need to add CHARACTER(9):: to the array constructor to set the length,
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thanks, I now understand what is happening but not what the rationale is behind it. If I replace the array constructor with
week(1) = 'Monday' week(2) = 'Tuesday' week(3) = 'Wednesday' week(4) = 'Thursday' week(5) = 'Friday' week(6) = 'Saturday' week(7) = 'Sunday'
then I don't need to use trim and I get the second result. From your reply the array constructor I used shouldn't compile so why isn't it's behaviour defined to be equivalent to individual assignments?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I did not say it shouldn't compile, I said it was nonstandard and that we defined a meaning for it as an extension. In an array of character values, all values have the same length. You have to go to a derived type array to get the behavior you want. as you found. It's not the same as individual assignments as then there is no requirement that the expressions have the same length.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Okay, I tried the following variation but hit a commpiler bug:
module daytime implicit none type day character(len=:), allocatable :: name contains procedure, private :: set_name_dtm procedure, private, pass(d_in) :: set_name_d generic, public ::assignment(=) => set_name_dtm, set_name_d end type day abstract interface subroutine print_day(this) import :: day type(day), intent(in) :: this end subroutine print_day end interface contains elemental subroutine set_name_d(d_out, d_in) type(day), intent(out) :: d_out class(day), intent(in) :: d_in d_out%name = d_in%name end subroutine set_name_d elemental subroutine set_name_dtm(this, n) class(day), intent(inout) :: this character(len=*), intent(in) :: n this%name = n ! trim(n) end subroutine set_name_dtm end module daytime program rdays use daytime implicit none integer, parameter :: ndays = 7 type(day) :: week(ndays) integer :: nargs, larg, i character (len=16) :: arg logical :: reversed = .false. procedure(print_day), pointer :: output nargs = command_argument_count() if (nargs == 1) then call get_command_argument(1, arg, larg) if (arg(1:larg) == 'rev') reversed = .true. end if week = [day('Monday'),day('Tuesday'),day('Wednesday'),day('Thursday'),day('Friday'),day('Saturday'),day('Sunday')] end program rdays
ifort daytime2_f.f90
daytime2_f.f90(1): catastrophic error: **Internal compiler error: internal abort** Please report this error along with the circumstances in which it occurred in a Software Problem Report. Note: File and line given may not be explicit cause of this error.
compilation aborted for daytime2_f.f90 (code 1)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Sigh. Ok, thanks - we'll check it out.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I can reproduce the internal compiler error up through version 15.0.1 but not in the current 15.0.2.
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page