Prev: Software for plotting in Fortranxx?
Next: INTRINSIC STATEMENT for functions overloaded for user defined types
From: e p chandler on 16 Sep 2009 07:29 On Sep 16, 2:25 am, nos...(a)see.signature (Richard Maine) wrote: > e p chandler <e...(a)juno.com> wrote: > > > > 3) I prefer to have the freedom of organizing my config file as > > > *I* (and also my not-so-Fortran-educated) users want it to be. > > > If I use NAMELIST, i am forcing me and my users to stick with the > > > namelists syntax. > > > So leave the original config file alone. Write a script that > > transforms that file into proper input for NAMELIST. I'm sure it would > > only be a few lines of Perl, AWK or whatever. [Sorry Fortran purists.] > > Yukk! It isn't that I am such a Fortran purist that I object to using > tools like Perl, AWK, or whatever. It is just that this seems like an > excessively complicated and roundabout solution. The OP has a file, form > already specified, and wants to read that file into a Fortran program, > which is simple to do as is. It just seems to me to add layers of > complication to add a separate step of transforming a file just so that > you can then use a different way of reading it in Fortran. > > I'm sure you could also use perl to compute x+y for two Fortran > variables x and y. Write them to a file from Fortran, have Perl process > the file and write its output to another file, which you then read in > Fortran. It isn't because I don't like Perl that I find that a strange > way to do things. It is because I like simplicity. > > -- > Richard Maine | Good judgment comes from experience; > email: last name at domain . net | experience comes from bad judgment. > domain: summertriangle | -- Mark Twain I see your point. [insert cartoon sound of being jabbed with a pin here.] How would you handle the process of matching name with value in Fortran? Suppose that there are 10 items and the IF .. THEN construct is becoming a pain. -- e
From: nmm1 on 16 Sep 2009 09:07 In article <1j63xzt.86o5031grwjueN%nospam(a)see.signature>, Richard Maine <nospam(a)see.signature> wrote: >e p chandler <epc8(a)juno.com> wrote: > >> > 3) I prefer to have the freedom of organizing my config file as >> > *I* (and also my not-so-Fortran-educated) users want it to be. >> > If I use NAMELIST, i am forcing me and my users to stick with the >> > namelists syntax. >> >> So leave the original config file alone. Write a script that >> transforms that file into proper input for NAMELIST. I'm sure it would >> only be a few lines of Perl, AWK or whatever. [Sorry Fortran purists.] > >Yukk! It isn't that I am such a Fortran purist that I object to using >tools like Perl, AWK, or whatever. It is just that this seems like an >excessively complicated and roundabout solution. The OP has a file, form >already specified, and wants to read that file into a Fortran program, >which is simple to do as is. It just seems to me to add layers of >complication to add a separate step of transforming a file just so that >you can then use a different way of reading it in Fortran. It's not just configuration files. The restrictions in Fortran are such that it is damn-near impossible to read free-format input of many common forms without unpicking the text by hand. C does a LOT better, and so did some earlier languages. Try CSV for an example. By removing the unnecessary restrictions, Fortran could be upgraded to the level of C, but there is no way that you can handle arbitrary input in either without unpicking the text by hand. Which is where languages that are better for the purpose, such as Python, Perl etc. are useful. I.e. it MAY add layers of complication, but that doesn't mean that it isn't the best way (in terms of programmer time and reliability) to do the job. Regards, Nick Maclaren.
From: Gordon Sande on 16 Sep 2009 10:35 On 2009-09-16 08:29:09 -0300, e p chandler <epc8(a)juno.com> said: > On Sep 16, 2:25�am, nos...(a)see.signature (Richard Maine) wrote: >> e p chandler <e...(a)juno.com> wrote: >> >>>> 3) I prefer to have the freedom of organizing my config file as >>>> *I* (and also my not-so-Fortran-educated) users want it to be. >>>> If I use NAMELIST, i am forcing me and my users to stick with the >>>> namelists syntax. >> >>> So leave the original config file alone. Write a script that >>> transforms that file into proper input for NAMELIST. I'm sure it would >>> only be a few lines of Perl, AWK or whatever. [Sorry Fortran purists.] >> >> Yukk! It isn't that I am such a Fortran purist that I object to using >> tools like Perl, AWK, or whatever. It is just that this seems like an >> excessively complicated and roundabout solution. The OP has a file, form >> already specified, and wants to read that file into a Fortran program, >> which is simple to do as is. It just seems to me to add layers of >> complication to add a separate step of transforming a file just so that >> you can then use a different way of reading it in Fortran. >> >> I'm sure you could also use perl to compute x+y for two Fortran >> variables x and y. Write them to a file from Fortran, have Perl process >> the file and write its output to another file, which you then read in >> Fortran. It isn't because I don't like Perl that I find that a strange >> way to do things. It is because I like simplicity. >> >> -- >> Richard Maine � � � � � � � � � �| Good judgment come > s from experience; >> email: last name at domain . net | experience comes from bad judgment. >> domain: summertriangle � � � � � | �-- Mark Twain > > I see your point. [insert cartoon sound of being jabbed with a pin > here.] How would you handle the process of matching name with value in > Fortran? Suppose that there are 10 items and the IF .. THEN construct > is becoming a pain. > > -- e It depends! ;-) If I expect to use the program myself for the rest of the week then I use NAMELIST. If I expect tolerant users to use the program for a short while then I ask for equals signs and I internally convert to NAMELIST. If I expect users to use the program for the next while (twenty years is all too common) then I will do a careful scan and conversion. Numbers can be bad in a large variety of ways. It helps to at least point at the offending text as often the error then becomes blindingly obvious to most users. I have a small library of this stuff so I do not have to do it again.
From: glen herrmannsfeldt on 16 Sep 2009 12:16 e p chandler <epc8(a)juno.com> wrote: < On Sep 16, 2:25?am, nos...(a)see.signature (Richard Maine) wrote: (snip) <> Yukk! It isn't that I am such a Fortran purist that I object to using <> tools like Perl, AWK, or whatever. It is just that this seems like an <> excessively complicated and roundabout solution. The OP has a file, form <> already specified, and wants to read that file into a Fortran program, <> which is simple to do as is. It just seems to me to add layers of <> complication to add a separate step of transforming a file just so that <> you can then use a different way of reading it in Fortran. (snip) < I see your point. [insert cartoon sound of being jabbed with a pin < here.] How would you handle the process of matching name with value in < Fortran? Suppose that there are 10 items and the IF .. THEN construct < is becoming a pain. A table and a loop wouldn't be so hard, which is likely what NAMELIST does. If it gets really big, a hash table, but that is probably not necessary. If you have a table of the name and a pointer to the variable, you can loop through the names and assign to the appropriate variable. If the variables aren't all the same type, that requires a generic pointer, which I believe came with F2003. Well, you could have separate tables for each type and a nested loop. -- glen
From: John on 16 Sep 2009 20:46
On Sep 15, 8:23 am, Bart Vandewoestyne <MyFirstName.MyLastN...(a)telenet.be> wrote: > Hello all, > > After switching jobs and now mainly being programming in Octave, > it is nice to be back for asking yet another questions here to > good old clf :-) > > I would like to read a configuration file that could look like > this: > > nb_apples 3 > my_weight 68.5 > elephant_weight 4.6e3 > smoking_allowed true > name TestName > > nb_apples, my_weight, elephant_weight, smoking_allowed and name > are variables (of the appropriate data type) in my Fortran 95 program. > During initialization of my program, I want to read the config > file and assign the variables the values that are specified in the config > file. > > Currently, I only work with integers and real numbers (no > logicals or character arrays yet) and I read my datafile like > this: > > read(unit=1, fmt=*, iostat=ios) variable_name, variable_value > > where variable_name is a character array and variable_value is > being read as a real. > > Using a very long switch/case statement, I then assign the value > to the variable depending on what i read in variable_name. If I > have an integer variable, I simply floor the real number and then > assign the value. So for now, I use 'little tricks' and can't > work with strings. > > Now... something inside of me tells me there's a better way that > would also allow me to assign logicals and strings. Therefore, I > would have to be able to read *only* variable_name, then use a > switch/case statement to decide what variable I'll have to > assign, and once i know what variable I'm working with, I could > read the second part of the line and assign it to the variable of > the correct data type. > > My question is then: if a line in my config file consists of two > things separated by whitespace, how do I read the first thing as > a character array and then a bit later the second thing of the > appropriate data-type? > > Thanks, > Bart > > -- > "Share what you know. Learn what you don't." The easiest way is to read the entire line into a character variable, find the end of the name, with something like index(line,' ') and then, knowing the name do an internal read from the remaining character substring. But ( assuming for simplicity no leading spaces on lines, that you are willing to quote text variable values, and that using .true. or T is OK for logical values (but if you want words like True and False you could read then as character variables and then set the logical value based on the string)) you can change your input so be NAMELIST input. The advantages are primarily that you do not have to have conditional code to select a corrrect read for each variable name or worry about case in your input; a simple example would be --------------------- ! define NAMELIST and members integer :: nb_apples=0 real :: my_weight=0.0 real :: elephant_weight=0.0 logical :: smoking_allowed=.false. character(len=20) :: name=' ' namelist/config/ nb_apples,my_weight,elephant_weight,smoking_allowed,name !--------------------------------------- ! line(1)='&config' line(3)='/' !--------------------------------------- do read(*,'(a)',iostat=ios)line(2) if(ios.ne.0)exit ii=index(line(2),' ') line(2)(ii:ii)='=' read(line,config) enddo write(*,config) ! print values end EOF ################################ g95 show.f90 -o show ################################ ../show <<\EOF nb_apples 3 my_weight 68.5 elephant_weight 4.6e3 smoking_allowed .true. smoking_allowed T name 'TestName' EOF ################################ exit ################################ &CONFIG NB_APPLES = 3, MY_WEIGHT = 68.5, ELEPHANT_WEIGHT = 4600., SMOKING_ALLOWED = T, NAME = 'TestName ', / It would be easy to trim the first word and to add error processing (especially use of IOMSG on the internal read of the NAMELIST if it is available on your compiler to make this more robust. |