Prev: MATLAB chrashes when loading FORTRAN dynamic library
Next: Passing overloaded function as argument to subroutine
From: Fortran_follower on 7 Jul 2010 11:31 Hi, I want to form a multi-dimension character array in fortran. the strings look like: abc_1242.doc asd_3294.txt wee_543112.txt ...... ..... I wrote: character*20 :: list*10 do i=1:10 read(50,'(a)')list(i) ! Here is the problem. How can I mention about the columns? enddo ! 'read' statement reads a line at a time. Or if any other simple way is there..plz let me know. Thank U.
From: m_b_metcalf on 7 Jul 2010 12:24 On Jul 7, 5:31 pm, Fortran_follower <ezeepravee...(a)gmail.com> wrote: > Hi, > I want to form a multi-dimension character array in fortran. > > the strings look like: > > abc_1242.doc > asd_3294.txt > wee_543112.txt > ..... > .... > > I wrote: > > character*20 :: list*10 > do i=1:10 > read(50,'(a)')list(i) ! Here is the problem. How can I mention > about the columns? > enddo ! 'read' statement reads a line at a > time. > > Or if any other simple way is there..plz let me know. > > Thank U. Try writing: character(20), dimension(10) :: list do i=1,10 read(50,'(a)')list(i) ! Here is the problem. How can I mention about the columns? enddo instead. (Your code respecifies the length as 10.) Regards, Mike Metcalf
From: Richard Maine on 7 Jul 2010 12:31 Fortran_follower <ezeepraveen4u(a)gmail.com> wrote: > I want to form a multi-dimension character array in fortran. > > the strings look like: > > abc_1242.doc > asd_3294.txt > wee_543112.txt Do not confuse strings with arrays. The two are not the same thing (at least not in Fortran). Some of the differences are suble, and it would be hard to list them all. Better just to start out with the realization that they are different and don't try to equate them. A string is composed of multiple characters, but it is a scalar. One can have a multi-dimensional array of characters, but that doesn't look at all like what you are describing. Looks to me like what you have is a 1-D array of strings. That's a resonably common thing. > character*20 :: list*10 There are no arrays at all in the declaration. The 20 and 10 are both declarations of string length (not array size). The 10 overrides the 20. There is not point in putting them both there; it doesn't do anything useful (unless causing confusion is considered useful). > do i=1:10 > read(50,'(a)')list(i) ! Here is the problem. How can I mention > about the columns? > enddo Don't even think about "columns". If you do so, that means you are still thinking of strings as arrays. Don't do that. Looks like you want an array of 10 strings, with the strings being of length 20. There are many ways to declare that. I'd probably be prone to write character*20 :: list(10) but there are a host of other forms, differing only in matters of style. The form you tried is not one of the allowed ones. It declared the string length twice, with one value overriding the other, rather than declaring a string length and a dimension. Your read loop ought to work fine with this declaration. For that matter, you could dispense with the loop and just write read(50,'(a)') list The read will go to a new record (line) for each element of the list. As an aside, I recommend against using literal constant unit numbers like the above 50. I would instead use an integer variable whose value is set to 50. -- Richard Maine | Good judgment comes from experience; email: last name at domain . net | experience comes from bad judgment. domain: summertriangle | -- Mark Twain
From: Fortran_follower on 8 Jul 2010 04:39
On Jul 7, 6:31 pm, nos...(a)see.signature (Richard Maine) wrote: > Fortran_follower <ezeepravee...(a)gmail.com> wrote: > > I want to form a multi-dimension character array in fortran. > > > the strings look like: > > > abc_1242.doc > > asd_3294.txt > > wee_543112.txt > > Do not confuse strings with arrays. The two are not the same thing (at > least not in Fortran). Some of the differences are suble, and it would > be hard to list them all. Better just to start out with the realization > that they are different and don't try to equate them. > > A string is composed of multiple characters, but it is a scalar. > > One can have a multi-dimensional array of characters, but that doesn't > look at all like what you are describing. Looks to me like what you have > is a 1-D array of strings. That's a resonably common thing. > > > character*20 :: list*10 > > There are no arrays at all in the declaration. The 20 and 10 are both > declarations of string length (not array size). The 10 overrides the 20. > There is not point in putting them both there; it doesn't do anything > useful (unless causing confusion is considered useful). > > > do i=1:10 > > read(50,'(a)')list(i) ! Here is the problem. How can I mention > > about the columns? > > enddo > > Don't even think about "columns". If you do so, that means you are still > thinking of strings as arrays. Don't do that. Looks like you want an > array of 10 strings, with the strings being of length 20. There are many > ways to declare that. I'd probably be prone to write > > character*20 :: list(10) > > but there are a host of other forms, differing only in matters of style. > The form you tried is not one of the allowed ones. It declared the > string length twice, with one value overriding the other, rather than > declaring a string length and a dimension. > > Your read loop ought to work fine with this declaration. For that > matter, you could dispense with the loop and just write > > read(50,'(a)') list > > The read will go to a new record (line) for each element of the list. > > As an aside, I recommend against using literal constant unit numbers > like the above 50. I would instead use an integer variable whose value > is set to 50. > > -- > Richard Maine | Good judgment comes from experience; > email: last name at domain . net | experience comes from bad judgment. > domain: summertriangle | -- Mark Twain Thanks to Richard Maine & Mike Metcalf for ur responses...it helped me a lot. |