简体   繁体   English

如何用erlang解析配置文件?

[英]How to parse config file with erlang?

i am developing one program with erlang, which need to read config file when starting, then load the config data to database. 我正在用erlang开发一个程序,它需要在启动时读取配置文件,然后将配置数据加载到数据库。 originally, using Ruby or C i can load YAML file or conf file. 最初,使用Ruby或C i可以加载YAML文件或conf文件。 I want to know, in erlang's world, is there any good way to load config file? 我想知道,在erlang的世界里,有没有什么好方法可以加载配置文件? thanks! 谢谢!

This is the YAML-Style file I need to load, and i do not care the style of the config file 这是我需要加载的YAML样式文件,我不关心配置文件的样式

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

If you store the config data as Erlang terms you can use the built-in file:consult/1 function to parse it. 如果将配置数据存储为Erlang术语,则可以使用内置文件:consult / 1函数来解析它。

How you structure your data is up to you. 您如何构建数据取决于您自己。 For example, you could use proplists: 例如,您可以使用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. 这取决于您的数据以及您希望如何访问它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM