我有像这样的列表
ItemListData = [[1,"ABC","DEF",30],[2,"AXN","FOO",23],[3,"BDE","FO1",21],..]
我希望使用记录Item{ItemNo, Name, Description, quantity}
形成记录列表。项目列表长度会有所不同。使用ItemListData
创建新记录列表的最佳方法是什么。
答案 0 :(得分:1)
@ graymalkin的答案涉及通过使用++
二元运算符进行大量复制。
人们应该更喜欢这里的列表理解:
make_records(Records) ->
[#item{'#'=No, name=Name, descr=Descr, qty=Qty}
|| [No,Name,Descr,Qty] <- Records].
编辑: 因此使用此功能:(在shell中)
1> rd(item, {'#', name, descr, qty}).
2> ItemListData = [[1,"ABC","DEF",30],[2,"AXN","FOO",23],[3,"BDE","FO1",21]].
3> some_module:make_records(ItemListData).
答案 1 :(得分:0)
这样的事情会......
make_records([], Records) -> Records;
make_records([[No,Name,Desc,Qty]|Xs], Records) ->
make_records(Xs, Records ++ Item{itemNo = No, name = Name, desc = Desc, qty = Qty}).
答案 2 :(得分:0)
你只需要这个:
rd(item, {num, name, descr, qty}).
L = [[1,"ABC","DEF",30],[2,"AXN","FOO",23],[3,"BDE","FO1",21]].
lists:map(fun([No, Name, Des, Qty]) -> #item{num = No, name=Name, descr=Des, qty=Qty} end, L).
答案 3 :(得分:0)
只需使用list_to_tuple:
rd(item, {num, name, descr, qty}),
Ls = [[1,"ABC","DEF",30],[2,"AXN","FOO",23],[3,"BDE","FO1",21]],
[list_to_tuple([item|L]) || L <- Ls].