开发者

Erlang How to get String from list

How can i convert List in String in erlang?

My list view:开发者_运维技巧

[{{19,59,51},{2011,1,14},"fff"},{{19,59,47},{2011,1,14},"ASDfff"}]

Thank you.


A very simple thing would be

List = [{{19,59,51},{2011,1,14},"fff"},
        {{19,59,47},{2011,1,14},"ASDfff"}],
IOList = io_lib:format("~w", [List]),
FlatList = lists:flatten(IOList),

but as these appear to be timestamps which you may want to be formatted in a better way, something like

FormattedIOLists =
   [ io_lib:format("~4..0B-~2..0B-~2..0B ~2..0B:~2..0B:~2..0B ~s",
                   [YYYY,M,D, HH,MM,SS, Comment])
     || {{HH,MM,SS},{YYYY,M,D},Comment} <- List ],
FormattedFlatLists =
   [ lists:flatten(io_lib:format("~4..0B-~2..0B-~2..0B ~2..0B:~2..0B:~2..0B ~s",
                                 [YYYY,M,D, HH,MM,SS, Comment]))
     || {{HH,MM,SS},{YYYY,M,D},Comment} <- List ],

could fit your bill better.

For quick and dirty interactive output on the shell,

9> [ io:format("~4..0B-~2..0B-~2..0B ~2..0B:~2..0B:~2..0B ~s~n", [YYYY,M,D, HH,MM,SS, Comment]) || {{HH,MM,SS},{YYYY,M,D},Comment} <- List ].                 
2011-01-14 19:59:51 fff
2011-01-14 19:59:47 ASDfff
[ok,ok]
10> lists:foreach(fun({{HH,MM,SS},{YYYY,M,D},Comment}) -> io:format("~4..0B-~2..0B-~2..0B ~2..0B:~2..0B:~2..0B ~s~n", [YYYY,M,D, HH,MM,SS, Comment]) end, List).
2011-01-14 19:59:51 fff
2011-01-14 19:59:47 ASDfff
11> 

Note that in most cases building recursive lists of lists (iolists) is a much better thing to do than flattening those iolists. Most output functions directly accept iolists for output data, so you gain nothing by flattening the lists before the actualy output happens.


Maybe just:

 io_lib:format("~w", [[{{19,59,51},{2011,1,14},"fff"},{{19,59,47},{2011,1,14},"ASDfff"}]]).
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜