From: Richard Maine on 25 Jan 2010 12:28 Fortran_follower <ezeepraveen4u(a)gmail.com> wrote: > Please have a look at my declaration below: > character,allocatable,dimension(:,:) :: fname > character*100 :: outfile,infile,outfname > character*150 :: infpath,outfpath,line > integer :: num,i,j,col > c > col=50 > i=1 > j=1 > c > write(*,*)'Enter no.of files to merge' > read(*,*)num > allocate(fname(num,col)) > c > > > outfname = [fname(1,1:7), fname(num,1:6)] > > For the solution you have mentioned, I am getting the following error. > outfname = [fname(1,1:7), fname(num,1:6)] > 1 > Error: Incompatible ranks 0 and 1 in assignment at (1) > > Can you please let me know...whether the problem is with the > declaration or with something else...!! Ah, Ok. Mostly in the declaration, though a bit of both. Yes, you do have a 2-D array of characters. One could make that work, but it would just cause a continuing series of complications. The "incompatible rank" error is because the right-hand-side of that assignment is now a valid expression, but it gives a 1-D character array, which you can't assign to a character string. One could change the declaration of outfname to be a character array, but that will just cause more problems later. To make your fname be a 1-D array of character strings, declare it something more like character*50, allocatable, dimension(:) :: name (I tried to stay with the general declaration style you used; variations of stylistic detail are possible.) You can't make the character length allocatable until full f2003 compilers are out (and that seems to be one of the later fetaures to be done in partial implementations), but it doesn't look to me like you really need the length to be allocatable; I'm guessing that was just forced on you by using the 2-D array and wanting the other rank allocatable. Then you'll have to change your allocate statement to correspond to fname, which is now a 1-D array, which the allocate needs to reflect. allocate(fname(num)) Then use the syntax shown in Jugoslav's post - namely outfname = fname(1)(1:7) // fname(num)(1:6) In brief explanation of that syntax, the (1) and (num) are the array index values, while the 1:7 and 1:6 are substring designations. Although substrings look a lot like array slices, they aren't the same thing. When you have both a substring and array index of the same variable, the two go into separate sets of parens like this. I'm also assuming that whatever you did to define values for fname will be appropriate for a 1-D array of strings. Odds are that it already is and that it in fact would not have worked correctly for the 2-D array you declared. (Just because it might have compiled doesn't mean it would have worked). -- Richard Maine | Good judgment comes from experience; email: last name at domain . net | experience comes from bad judgment. domain: summertriangle | -- Mark Twain
From: James Van Buskirk on 25 Jan 2010 12:28 "Fortran_follower" <ezeepraveen4u(a)gmail.com> wrote in message news:3a33e589-6416-49cc-9279-4227ede8ec3e(a)y12g2000yqh.googlegroups.com... > For the solution you have mentioned, I am getting the following error. > outfname = [fname(1,1:7), fname(num,1:6)] > 1 > Error: Incompatible ranks 0 and 1 in assignment at (1) outfname = transfer([fname(1,1:7), fname(num,1:6)],repeat('x',7+6)) -- write(*,*) transfer((/17.392111325966148d0,6.5794487871554595D-85, & 6.0134700243160014d-154/),(/'x'/)); end
From: e p chandler on 25 Jan 2010 21:34 "Fortran_follower" <ezeepraveen4u(a)gmail.com> wrote in message news:dd63bcc5-aa09-411d-a44c-ff4815e2e10b(a)v25g2000yqk.googlegroups.com... > Dear Richard Maine, > > Thank you for your detailed reply to my query. > Please have a look at my declaration below: > character,allocatable,dimension(:,:) :: fname > character*100 :: outfile,infile,outfname > character*150 :: infpath,outfpath,line > integer :: num,i,j,col > c > col=50 > i=1 > j=1 > c > write(*,*)'Enter no.of files to merge' > read(*,*)num > allocate(fname(num,col)) > c > >> outfname = [fname(1,1:7), fname(num,1:6)] > > For the solution you have mentioned, I am getting the following error. > outfname = [fname(1,1:7), fname(num,1:6)] > 1 > Error: Incompatible ranks 0 and 1 in assignment at (1) > > Can you please let me know...whether the problem is with the > declaration or with something else...!! > > Thanking you. > > Praveen. outfname is a character variable (string). fname(1,1:7) is an array slice. It is NOT a string of characters. outfname='ABCDEFG' is not the same as outfname = ['A','B','C','D','E','F','G'] --- e e-mail: epc8 at juno dot com
From: Gordon Sande on 26 Jan 2010 08:28 On 2010-01-25 22:34:59 -0400, "e p chandler" <epc8(a)juno.com> said: > > "Fortran_follower" <ezeepraveen4u(a)gmail.com> wrote in message > news:dd63bcc5-aa09-411d-a44c-ff4815e2e10b(a)v25g2000yqk.googlegroups.com... Dear > >> Richard Maine, >> >> Thank you for your detailed reply to my query. >> Please have a look at my declaration below: >> character,allocatable,dimension(:,:) :: fname >> character*100 :: outfile,infile,outfname >> character*150 :: infpath,outfpath,line >> integer :: num,i,j,col >> c >> col=50 >> i=1 >> j=1 >> c >> write(*,*)'Enter no.of files to merge' >> read(*,*)num >> allocate(fname(num,col)) >> c >> >>> outfname = [fname(1,1:7), fname(num,1:6)] >> >> For the solution you have mentioned, I am getting the following error. >> outfname = [fname(1,1:7), fname(num,1:6)] >> 1 >> Error: Incompatible ranks 0 and 1 in assignment at (1) >> >> Can you please let me know...whether the problem is with the >> declaration or with something else...!! >> >> Thanking you. >> >> Praveen. > > outfname is a character variable (string). fname(1,1:7) is an array > slice. It is NOT a string of characters. > > outfname='ABCDEFG' is not the same as outfname = ['A','B','C','D','E','F','G'] Is there a nice idiom for doing the conversion? On the few occasions I have had to do this I ended up with a loop assigninng single characters. There are special rules on the use of character arrays as formats in i/o that do the conversion for you. Either that is a slick feature for that case or an indication of a missing ability elsewhere. Nice does not include the use of transfer or changing all the commas above to concatenate symbols. (I tend to rate transfer as "too clever for words" and to be avoided.) > --- e > e-mail: epc8 at juno dot com
From: e p chandler on 26 Jan 2010 10:32 "Gordon Sande" <g.sande(a)worldnet.att.net> wrote in message news:2010012609284916807-gsande(a)worldnetattnet... > On 2010-01-25 22:34:59 -0400, "e p chandler" <epc8(a)juno.com> said: > >> >> "Fortran_follower" <ezeepraveen4u(a)gmail.com> wrote in message >> news:dd63bcc5-aa09-411d-a44c-ff4815e2e10b(a)v25g2000yqk.googlegroups.com... > Dear >> >>> Richard Maine, >>> >>> Thank you for your detailed reply to my query. >>> Please have a look at my declaration below: >>> character,allocatable,dimension(:,:) :: fname >>> character*100 :: outfile,infile,outfname >>> character*150 :: infpath,outfpath,line >>> integer :: num,i,j,col >>> c >>> col=50 >>> i=1 >>> j=1 >>> c >>> write(*,*)'Enter no.of files to merge' >>> read(*,*)num >>> allocate(fname(num,col)) >>> c >>> >>>> outfname = [fname(1,1:7), fname(num,1:6)] >>> >>> For the solution you have mentioned, I am getting the following error. >>> outfname = [fname(1,1:7), fname(num,1:6)] >>> 1 >>> Error: Incompatible ranks 0 and 1 in assignment at (1) >>> >>> Can you please let me know...whether the problem is with the >>> declaration or with something else...!! >>> >>> Thanking you. >>> >>> Praveen. >> >> outfname is a character variable (string). fname(1,1:7) is an array >> slice. It is NOT a string of characters. >> >> outfname='ABCDEFG' is not the same as outfname = >> ['A','B','C','D','E','F','G'] > > Is there a nice idiom for doing the conversion? Not AFAIK. It would be "nice" from the point of view of letting the OP do what he thinks he wants to do to have strings be the same as 1-D character vectors, as they are in APL, or have an APL like catenate function. But then what does catenate mean for a numeric array? A <- 1 2 3 4 5 I <- ,A I 12345 ????? > On the few occasions I have had > to do this I ended up with a loop assigninng single characters. There are > special > rules on the use of character arrays as formats in i/o that do the > conversion for > you. Either that is a slick feature for that case or an indication of a > missing > ability elsewhere. Yes. But that is deceptive. It's an artifact of the output being serialized. So printing A$ is the same as printing B[1:7]. > Nice does not include the use of transfer or changing all the commas above > to > concatenate symbols. (I tend to rate transfer as "too clever for words" > and to > be avoided.) Well JVB does write some "nice" write only code with it. [smile] BUT, I think we are missing the point here, aside from giving the OP a better conceptual understading of Fortran. [smile]. Why use a 2-D character array when an array of strings will do? Why use an array of strings when simple strings will do? IMO new programmers tend to want to keep everything in memory. For example, I remember a program posted in this newsgroup which solved a system of differential equations. It used one dimension of the array for the iteration number. It could have used just two arrays, one for the system's current state and one for the system's new state. Then copy new_state to old_state as an array operation. --- e
First
|
Prev
|
Next
|
Last
Pages: 1 2 3 Prev: should not g95 be downward compatible Next: Another class of "DO10I=" bug |