在Erlang中将从文件读取的行拆分为键值对数组



我有一个包含N个键值对的txt文件:

key_1=val_1
key_2=val_2
key_N=val_N

我需要读取这个文件并将其设置为数组:

Attributes = [
{key_1, val_1},
{key_2, val_2},
{key_N, val_N}],

我尝试了几种不同的方法,但都没有成功,我得到的最接近的方法是:

{ok, FileAttributes} = file:read_file("/data/file-attributes.txt"),
string:split(FileAttributes, "=", all)

但当我打印这个时,我会得到:

key_1val_1
key_2val_2
key_Nval_N

读取file-attributes.txt并将键和值拆分为Attributes数组的项的最佳方式是什么?

我有一个包含N个键值对的txt文件:

key_1=val_1
key_2=val_2
key_N=val_N

我需要读取这个文件并将其设置为数组:

Attributes = [
{key_1, val_1},
{key_2, val_2},
{key_N, val_N}],
make_array(FileName) ->
case file:read_file(FileName) of
{ok, Bin} ->
Lines = binary:split(Bin, <<"n">>, [trim, global]),
Array = make_tuples(Lines, []),
io:format("Result=~n~p~n", [Array]);
{error, Reason} ->
io:format("[Me] There was an error:~n~w", [Reason])
end.
make_tuples([Line|Lines], Acc) ->
io:format("Line= -->~s<--~n", [Line]),
[Left, Right] = binary:split(Line, <<"=">>),
io:format("Term={~s, ~s}~n", [Left, Right]),
make_tuples(Lines, [{Left, Right}|Acc]);
make_tuples([], Acc) -> lists:reverse(Acc).

--输出:--

58> c(a).                               
a.erl:2:2: Warning: export_all flag enabled - all functions will be exported
%    2| -compile(export_all).
%     |  ^
{ok,a}
59> a:make_array("file-attributes.txt").
Line= -->key_1=val_1<--
Term={key_1, val_1}
Line= -->key_2=val_2<--
Term={key_2, val_2}
Line= -->key_N=val_N<--
Term={key_N, val_N}
Result=
[{<<"key_1">>,<<"val_1">>},
{<<"key_2">>,<<"val_2">>},
{<<"key_N">>,<<"val_N">>}]
ok

匹配示例:

-module(a).
-compile(export_all).
go([{1, Y, Z}=Tuple | Tuples]) ->
io:format("~w -> 2nd + 3rd = ~w~n", [Tuple, Y+Z]),
go(Tuples);
go([{X, 2, Z}=Tuple | Tuples]) ->
io:format("~w -> 1st + 3rd = ~w~n", [Tuple, X+Z]),
go(Tuples);
go([{X, Y, 3}=Tuple | Tuples]) ->
io:format("~w -> 1st + 2nd = ~w~n", [Tuple, X+Y]),
go(Tuples);
go([{X, Y, Z}=Tuple | Tuples]) ->
io:format("~w -> Sum = ~w~n", [Tuple, X+Y+Z]),
go(Tuples);
go([]) -> all_done.
test() ->
go([
{1, 2, 3},
{3, 2, 1},
{3, 4, 3},
{10, 20, 30}
]).

--输出:--

14> c(a).    
a.erl:2:2: Warning: export_all flag enabled - all functions will be exported
%    2| -compile(export_all).
%     |  ^
{ok,a}
15> a:test().
{1,2,3} -> 2nd + 3rd = 5
{3,2,1} -> 1st + 3rd = 4
{3,4,3} -> 1st + 2nd = 7
{10,20,30} -> Sum = 60
all_done

一个包含一个元素的列表可以写成:

[ 4 | [] ]

因此,如果您有以下函数子句:

go( [Head|Tail] ) -> 
%% do stuff with Head
go(Tail).

并呼叫:

g([4]).

erlang将把4分配给Head,把空列表[]分配给Tail。但是,当Tail是空列表时,对go(Tail)的调用将与该函数子句不匹配,从而导致程序崩溃。

此外,请注意,与Java不同,函数头可以将常量和变量混合用于参数。

最新更新