将一串列表转换为erlang中的列表

时间:2018-03-15 03:25:00

标签: string list type-conversion erlang

到目前为止,我找不到任何与我有同样问题的人:

  • 输入:" [' atom_with_special_CHARACTERS_like @ 123']"
  • 输出:[' atom_with_special_CHARACTERS_like @ 123']

就是这样,但是在今天早上到中午之后,尝试混合类似:string:tokens,list_to_tuple,erl_parse ......我找不到任何解决方案...... 我知道我已接近输出,但实际上无法完成它。 请你让我有点想法吗?

2 个答案:

答案 0 :(得分:3)

1> Parse = fun(S) -> {ok, Ts, _} = erl_scan:string(S), {ok, Result} = erl_parse:parse_term(Ts ++ [{dot,1} || element(1, lists:last(Ts)) =/= dot]), Result end.
#Fun<erl_eval.6.99386804>
2> L = "['atom_with_special_CHARACTERS_like@123', 'you mean[like, this%']".
"['atom_with_special_CHARACTERS_like@123', 'you mean[like, this%']"
3> Parse(L).
[atom_with_special_CHARACTERS_like@123,
 'you mean[like, this%']
4> Parse("  [foo, bar,\n baz, 'q u ux'   ]   ").
[foo,bar,baz,'q u ux']
5> Parse("{you, [can, 'write any', term, 123, 3.5, yep]}").
{you,[can,'write any',term,123,3.5,yep]}
6> Parse("even_end_with_dot.").                            
even_end_with_dot

答案 1 :(得分:1)

这是我的解决方案:

drop_first_last(Str) ->
    lists:reverse(tl(lists:reverse(tl(Str)))).

parse(Str) ->
    R = drop_first_last(Str), 
    [list_to_atom(drop_first_last(E)) || E <-string:tokens(R,",")].

调用解析函数:

pokus:parse("['atom_with_special_CHARACTERS_like@123']").

输出:

[atom_with_special_CHARACTERS_like@123]

或具有多个值:

pokus:parse("
['atom_with_special_CHARACTERS_like@123','another_atom@111']").

输出:

[atom_with_special_CHARACTERS_like@123,another_atom@111]