- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I want the leters a-h to go into separate cells in the C1 array.
character*8 c8
character*1 c1(8)
data c8/"ABCDEFGH"/
c1(1:8)=c8
c1(1:8)=c8(1:8) ! does not work
c1=c8(1:8) ! does not work
print *,c1
end
Other than a DO LOOP, how can I do this?
I think the compiler gets confused - - -
Link Copied
3 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
You could use a forall statement. This is a flexible form of assignment.
forall (i=1:size(c1)) c1(i) = c8(i:i)
Or, you could use an array constructor:
c1 = [(c8(i:i), i=1, size(c1))]
Whether the above are better or worse than a DO construct is probably down to personal preference. In both i is of type integer.
Background for what you've observed, just for clarity:
- c8 is a scalar with length 8. c1 is an array of size 8 with length 1. All elements in an array have the same length.
- When you assign an scalar to an array all elements of the array are set to the scalar. You have the equivalent of c1(1) = c8 ; c1(2) = c8 ; ... ; c1(8) = c8.
- Character assignment truncates or pads to fit the destination variable - consequently your three examples would have set each element of c1 to be 'A'.
forall (i=1:size(c1)) c1(i) = c8(i:i)
Or, you could use an array constructor:
c1 = [(c8(i:i), i=1, size(c1))]
Whether the above are better or worse than a DO construct is probably down to personal preference. In both i is of type integer.
Background for what you've observed, just for clarity:
- c8 is a scalar with length 8. c1 is an array of size 8 with length 1. All elements in an array have the same length.
- When you assign an scalar to an array all elements of the array are set to the scalar. You have the equivalent of c1(1) = c8 ; c1(2) = c8 ; ... ; c1(8) = c8.
- Character assignment truncates or pads to fit the destination variable - consequently your three examples would have set each element of c1 to be 'A'.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
What you say makes sense -
But I thought that using the range info (on C8) would modify the rule about that.
Apparently not, however.
Thanks ! ! !
But I thought that using the range info (on C8) would modify the rule about that.
Apparently not, however.
Thanks ! ! !
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
c1 = transfer(c8,c1)

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