【问题标题】:convert a string of list to a list in erlang将列表字符串转换为erlang中的列表
【发布时间】:2018-08-23 18:29:17
【问题描述】:

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

  • 输入:“['atom_with_special_CHARACTERS_like@123']”
  • 输出:['atom_with_special_CHARACTERS_like@123']

仅此而已,但是在整个上午到中午之后,尝试混合以下内容:string:tokens,list_to_tuple,erl_parse...我找不到任何解决方案... 我知道我正在接近输出,但无法真正完成它。 请你给我一些想法好吗?

【问题讨论】:

    标签: string list type-conversion erlang


    【解决方案1】:
    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
    

    【讨论】:

    • 谢谢!!这确实是我需要的。 :) 谢谢,祝你有美好的一天。
    【解决方案2】:

    这是我的解决方案:

    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]
    

    【讨论】:

      猜你喜欢
      • 2015-04-20
      • 1970-01-01
      • 1970-01-01
      • 2011-04-19
      • 2020-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-18
      相关资源
      最近更新 更多