Read a configuration file: Difference between revisions

Replaced “f.lines” with “"demo.config.lines()” avoiding expliciting "f". Replaced the OrderedTableRef with an OrderedTable. Some other minor changes.
m (→‎{{header|Phix}}: added syntax colouring the hard way)
(Replaced “f.lines” with “"demo.config.lines()” avoiding expliciting "f". Replaced the OrderedTableRef with an OrderedTable. Some other minor changes.)
Line 3,116:
=={{header|Nim}}==
<lang Nim>import re, strformat, strutils, tables
 
var configs: = newOrderedTableOrderedTable[string, seq[string]]()
var f = open("demo.config") ## shown above in description of the task
var parsed: seq[string]
 
for line in f"demo.config".lines():
iflet not (line.startswith(re"#|;") or= line.strip() == ""):
if line parsed != "" and not line.strip().splitstartswith(re"\s*=\s*#|\s+;", 1):
if len(parsed) = line.split(re"\s*=\s*|\s+", 1:)
configs[parsed[0].toLower()] = if len(parsed) > 1: parsed[1].split(re"\s*,\s*") else: @[]
 
else:
for key in ["fullname", "favouritefruit", "needspeeling", "seedsremoved", "otherfamily"]:
configs[parsed[0].toLower()] = parsed[1].split(re"\s*,\s*")
f.close()
for key in ["fullname", "favouritefruit", "needspeeling", "seedsremoved",
"otherfamily"]:
if not configs.hasKey(key):
echo(&"{key} = false")
Line 3,141 ⟶ 3,137:
else:
for i, v in configs[key].pairs():
echo(&"{key}({i+1}) = {v}")</lang>
</lang>
{{out}}
<pre>
Anonymous user