如何用erlang解析配置文件? [英] How to parse config file with erlang?

查看:192
本文介绍了如何用erlang解析配置文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用erlang开发一个程序,在启动时需要读取配置文件,然后将配置数据加载到数据库。最初使用Ruby或C我可以加载YAML文件或者conf文件。我想知道,在erlang的世界里,有没有什么好的方法来加载配置文件?谢谢!



这是我需要加载的YAML样式文件,我不在意配置文件的样式

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


解决方案

如果您将配置数据存储为Erlang条款,则可以使用内置的文件:咨询/ 1 功能来解析它。



你的数据结构取决于你。例如,您可以使用proplists:

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

或键键值作为原子列表的键值元组:

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

或使用键作为字符串/ charlists:

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

或用键作为原子:

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

等等。这取决于您的数据和您想要访问的数据。


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

解决方案

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.

这篇关于如何用erlang解析配置文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆