【问题标题】:Erlang: Printing out specific value in listErlang:打印出列表中的特定值
【发布时间】:2015-11-16 10:03:59
【问题描述】:

如果我有一个列表,例如:

[{list1, [1,2]},{list2, [3,4]}]

如果将 [3,4] 作为变量传入,我将如何使用 io:format 打印出 [3,4],例如 I。

我目前正在做:

io:format("list 2: ~w~n", [I]),

【问题讨论】:

    标签: list printing erlang output


    【解决方案1】:

    您的示例列表格式为:[{Key1, Value1}, {Key2, Value2}, ...],其中 Key 是一个原子。这种列表也可以称为proplist(属性列表)。名为proplist 的模块可以完全处理这种数据结构。

    在你的情况下,你可以运行:

    PList = [{list1, [1,2]},{list2, [3,4]}],
    Value = proplists:get_value(list2, PList),
    io:format("list2: ~p~n", [Value]).
    

    变量Value 现在绑定到值[3,4]

    另请参阅:The Erlang-Documentation page for proplists

    【讨论】:

    • @Scott:您如何尝试打印值?如果我在控制台上运行它,它就可以正常工作了。
    • 这是我的错字,解决方案非常有用,谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-02-19
    • 2011-06-15
    • 2016-08-09
    • 2015-12-16
    • 2018-05-05
    • 1970-01-01
    • 2023-01-22
    相关资源
    最近更新 更多