Jump to content

Update a configuration file: Difference between revisions

(→‎{{header|Perl 6}}: fix bug & copy-paste'o)
(→‎{{header|Erlang}}: Add Fortran.)
Line 543:
Wrote this line: NUMBEROFBANANAS 1024
</pre>
 
=={{header|Fortran}}==
Fortran has long had a built-in method for writing and reading a configuration file with ease, via the NAMELIST facility. The designers of the modern "configuration" files have paid not the slightest attention to the protocol, which is as follows: <lang Fortran> PROGRAM TEST !Define some data aggregates, then write and read them.
CHARACTER*28 FAVOURITEFRUIT
LOGICAL NEEDSPEELING
LOGICAL SEEDSREMOVED
INTEGER NUMBEROFBANANAS
NAMELIST /FRUIT/ FAVOURITEFRUIT,NEEDSPEELING,SEEDSREMOVED,
1 NUMBEROFBANANAS
INTEGER F !An I/O unit number.
F = 10 !This will do.
 
Create an example file to show its format.
OPEN(F,FILE="Basket.txt",STATUS="REPLACE",ACTION="WRITE", !First, prepare a recipient file.
1 DELIM="QUOTE") !CHARACTER variables will be enquoted.
FAVOURITEFRUIT = "Banana"
NEEDSPEELING = .TRUE.
SEEDSREMOVED = .FALSE.
NUMBEROFBANANAS = 48
WRITE (F,FRUIT) !Write the lot in one go.
CLOSE (F) !Finished with output.
Can now read from the file.
OPEN(F,FILE="Basket.txt",STATUS="OLD",ACTION="READ", !Get it back.
1 DELIM="QUOTE")
READ (F,FRUIT) !Read who knows what.
WRITE (6,FRUIT)
END</lang>
Which produces a file in a standard layout.
<pre>
&FRUIT
FAVOURITEFRUIT = "Banana ",
NEEDSPEELING = T,
SEEDSREMOVED = F,
NUMBEROFBANANAS = 48
/
</pre>
Evidently, the variables names in the NAMELIST are written out, one to a line in free-format with a style appropriate to its type, much as if they were assignment statements within a source file. The namelist's name starts the output, and the block of values ends with a line starting with a slash. On input, blocks will be skipped until the correct block name is found, so a single file may contain parameters for multiple usages. All output starts with column two so that column one can be used as a carriage control character if output is to go to a lineprinter. There is special provision for an array's value list whereby a run of equal values can be noted via <code>''n''*''value''</code> to save space, further, an input statement for an array can name a single element (the others being unaffected), and with F90, the usual array span facilities are available as in <code>A(6:10) = 5*3.14</code>
 
The names may appear in any order, and names can be omitted - if so, the corresponding variable will be unaffected by a READ. This file could of course be edited before being read back in. F90 introduces the ! character as an "escape" comment and this is recognised in the NAMELIST input, outside of text literals of course.
 
Naturally, a parameter file could contain data in whatever format desired: such a file could be read and its components extracted via suitable code then written in the NAMELIST style for the actual internalisation of values. The point of this is that a text name of a variable is associated with the actual computer variable via the NAMELIST facility, the programmer need not slog through some endless CASE statement on the names of the variables. This process would be reversed for output.
 
But given that the NAMELIST protocol is available without difficulty, why would a hard-core fortranner bother for a Fortran programme?
 
=={{header|Go}}==
1,220

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.