Lua中的表格合并

如何将一个变长的LUA表格合并到另一个LUA表格中。

在FOR循环中,我想搜索记录(plunr和pluname)并将其放入表格中。 如果每个记录都放在表格中,则将该表格合并到另一个“请求表”中

问题在于只有最后一条记录被发送到Web服务器。

 for rrplu in receipt:getPLUs() do
   local plunr = rrplu:getPLUNo();
   local pluname = rrplu:getName();
   plutable = { tag = "hot1:TicketDetail",
                                    { tag = "hot1:PLU", plunr},
                                    { tag = "hot1:Description", pluname},
                                }

 end;

 local soap_client = vpos.communication.SOAPClient (http)
 local request = {
    url = urlHTTPS,
    soapaction = "http://blablabla.com/IXmlPosService/Charge",
    namespace = {hot="http://blablabla.com/",
    hot1="http://schemas.datacontract.org/2004/07/BlaBla.Web.DataContracts"},
    method = "hot:Charge",
    body =  { tag = "hot:Charge",
                    { tag = "hot:authentication",
                    { tag = "hot1:Token", TokenValue},
                    },
                    { tag = "hot:request",
                    { tag = "hot1:PosDate", datum},
                    { tag = "hot1:TicketDetails",
                    plutable
                    },
                    },
                    }
                }
   result, err, err_string = soap_client:call (request)
点赞
用户2226988
用户2226988

每次循环都会将 plutable 设置为一个新的表格。你可能想要插入到一个新表格或现有的表格中。

请使用以下代码替换 plutable =

plutable = plutable or {}
table.insert(plutable, {
    tag = "hot1:TicketDetail",
    { tag = "hot1:PLU", plunr },
    { tag = "hot1:Description", pluname }})
2017-10-12 15:01:19