How to parse config file with erlang?

♀尐吖头ヾ 提交于 2019-12-22 02:22:05

问题


i am developing one program with erlang, which need to read config file when starting, then load the config data to database. originally, using Ruby or C i can load YAML file or conf file. I want to know, in erlang's world, is there any good way to load config file? thanks!

This is the YAML-Style file I need to load, and i do not care the style of the config file

a:
  a1:
    a2: 1
    a3: 2
b:
  b1:
    b2: 3
    b3: 4

回答1:


If you store the config data as Erlang terms you can use the built-in file:consult/1 function to parse it.

How you structure your data is up to you. For example, you could use proplists:

{a, [{a1, [{a2, 1}, {a3, 2}]}]}.
{b, [{b1, [{b2, 3}, {b3, 4}]}]}.

Or key-value tuples with keys as lists of atoms:

{[a, a1, a2], 1}.
{[a, a1, a3], 2}.
{[b, b1, b2], 3}.
{[b, b1, b4], 4}.

Or with keys as strings/charlists:

{"a.a1.a2", 1}.
{"a.a1.a3", 2}.
{"b.b1.b2", 3}.
{"b.b1.b4", 4}.

Or with keys as atoms:

{a.a1.a2, 1}.
{a.a1.a3, 2}.
{b.b1.b2, 3}.
{b.b1.b4, 4}.

And so on. It depends on your data and how you want to access it.



来源:https://stackoverflow.com/questions/5927401/how-to-parse-config-file-with-erlang

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!