Prev: Software for plotting in Fortranxx?
Next: INTRINSIC STATEMENT for functions overloaded for user defined types
From: Bil Kleb on 15 Sep 2009 12:21 Bart Vandewoestyne wrote: > Hmm... i don't think I will use the namelist option, for three > reasons: > > 1) On > http://www.pcc.qub.ac.uk/tec/courses/f77tof90/stu-notes/f90studentMIF_7.html > is written: > > [...] It has now been > included in the Fortran 90 language. However, NAMELIST is a > poorly designed feature and should be avoided whenever possible." Interesting. I find NAMELIST to be one of the few designs in the Fortran language that actually adheres to the Open/Closed Principle.^1 My only complaint with NAMELISTs are the very poor error handling diagnostics it seems to provide. Then again, I haven't fully investigated them yet. My current ignorant complaint: you don't get detailed information about where a particular namelist read went awry, e.g., a misspelled variable or bad syntax, just a "namelist read failed" error code. > 2) I'm not sure, but i think using NAMELIST would make it harder > for me to allow comments starting with # in my config file... Yeah, you'd have to use Fortran's '!' for comments (and you'd have to use the 14-year old F95 instead of the 19-year old F90 to portably use them). > 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. Fair enough, but the changes necessary are very minimal to that given in your example config file. Without writing a test, I'd guess something like, &configure_case ! format: http://lmgtfy.com/?q=fortran+namelist nb_apples = 3 my_weight = 68.5 ! in kg, but should be called my_mass? elephant_weight = 4.6e3 smoking_allowed = .true. name = 'TestName' / and you could set defaults to that only those variables present would override the defaults. On NAMELIST's side, Google, compiler vendors, and Fortran books have plenty of documentation of their use, format, and troubleshooting whereas if you roll you own, you have to write the documentation. > But thanks for the suggestion! 'welcome. Thanks for the "should be avoided whenever possible" lead. Regards, -- http://twitter.com/bil_kleb [1] http://en.wikipedia.org/wiki/Open/closed_principle
From: Bil Kleb on 15 Sep 2009 12:25 nmm1(a)cam.ac.uk wrote: > Snurfl! That's pretty close to what I say in slide 21 of: > > http://www-uxsup.csx.cam.ac.uk/courses/OldFortran/full.pdf Please elucidate. I find only a vague, "[h]as some arcane restrictions on its use". Regards, -- Bil Kleb http://fun3d.larc.nasa.gov
From: Richard Maine on 15 Sep 2009 12:30 <nmm1(a)cam.ac.uk> wrote: > In article <WxOrm.143751$LX3.72405(a)newsfe17.ams2>, > Bart Vandewoestyne <MyFirstName.MyLastName(a)telenet.be> wrote: > > > >Hmm... i don't think I will use the namelist option, for three > >reasons: >... > Snurfl! That's pretty close to what I say in slide 21 of: > > http://www-uxsup.csx.cam.ac.uk/courses/OldFortran/full.pdf Oddly, I largely stopped using namelist right about when it became standardized. It wasn't so much because I particularly disaprove of namelist as it was a combination of two things. 1. I wanted more flexibility than namelist gives. I'll not go into details here, as there are just so many ways in which roll-your-own is more flexible than namelist. 2. Sometime around then, I built a personal library of procedures that facilitated rolling my own input parsing. This was nothing particularly fancy or esoteric - mostly just routines to do the kinds of things that always come up: read a "command" handling a line continuation convention, extract the next field using blanks or other field delimiters, convert a string to a real or integer with error checking, etc. -- Richard Maine | Good judgment comes from experience; email: last name at domain . net | experience comes from bad judgment. domain: summertriangle | -- Mark Twain
From: Dave Allured on 15 Sep 2009 13:21 Bart Vandewoestyne 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? There was a good exchange of ideas about this back in February, under the subject "reading configuration files". --Dave
From: nmm1 on 15 Sep 2009 13:30
In article <h8of62$g6j$2(a)news.eternal-september.org>, Bil Kleb <Bil.Kleb(a)nasa.gov> wrote: >nmm1(a)cam.ac.uk wrote: >> Snurfl! That's pretty close to what I say in slide 21 of: >> >> http://www-uxsup.csx.cam.ac.uk/courses/OldFortran/full.pdf > >Please elucidate. > >I find only a vague, "[h]as some arcane restrictions on its use". I also pointed out that it was an archaic feature that got put into Fortran 90! For its arcane restrictions, read the standard! A search on NAMELIST brings them up easily enough. Things like not being able to use arguments (like EQUIVALENCE), not on internal files, DELIM ignored for input, and a large number of minor but potentially puzzling differences from list-directed I/O on output. Regards, Nick Maclaren. |