lua – 逐行读取文件到数组中
发布时间:2020-12-14 21:43:20 所属栏目:大数据 来源:网络整理
导读:对不起,我还在学习lua.你可以纠正我,为什么文件中的数据不会逐行读取? 这是我在文件points.txt中的示例数据: lexxo:30:1rey:40:2lion:40:2prince:50:3royal:50:3 所以当我从上面得到的是2d阵列(表) player = {{(name),(points),(which var point earned on
对不起,我还在学习lua.你可以纠正我,为什么文件中的数据不会逐行读取?
这是我在文件points.txt中的示例数据: lexxo:30:1 rey:40:2 lion:40:2 prince:50:3 royal:50:3 所以当我从上面得到的是2d阵列(表) player = {{(name),(points),(which var point earned on index)},{(...),(...),(...)}}; 所以问题是,当我尝试循环打印文件中的所有数据.它只打印最新的一行.所以我想要打印所有这些 line_points = {} player_data = {{}} local rfile = io.open("points.txt","r") for line in rfile:lines() do playername,playerpoint,playeridpoint = line:match("^(.-):(%d+):(%d+)$") player_data = {{playername,playeridpoint}} line_points[#line_points + 1] = player_data end for i = 1,#player_data do player_checkname = player_data[i][1] -- Get Player Name From Array for checking further player_checkpnt = player_data[i][3] -- Get Player ID Point From Array for checking further print(i..". Name: "..player_data[i][1].." Point: ".. player_data[i][2] .. " ID: " .. player_data[i][3]); end 解决方法
player_data总是有索引1,因为你没有向它添加项目,你将它们添加到line_points,其中#line_points是5,所以请改用它.
那是你想要的吗? line_points = {} player_data = {{}} --I think you can delete it at all... --Because it is rewriting each time. local rfile = io.open("points.txt",playeridpoint = line:match("^(.-):(%d+):(%d+)$") player_data = {playername,playeridpoint} --I also remover double table here ^^^^^^^^^^^^^^^^^^^ line_points[#line_points + 1] = player_data end --Here i checked counts --print('#pd='..#player_data) --print('#lp='..#line_points) --After it i decided to use line_points instead of player_data for i = 1,#line_points do player_checkname = line_points[i][1] -- Get Player Name From Array for checking further player_checkpnt = line_points[i][3] -- Get Player ID Point From Array for checking further print(i..". Name: "..line_points[i][1].." Point: ".. line_points[i][2] .. " ID: " .. line_points[i][3]); end 输出: 1. Name: lexxo Point: 30 ID: 1 2. Name: rey Point: 40 ID: 2 3. Name: lion Point: 40 ID: 2 4. Name: prince Point: 50 ID: 3 5. Name: royal Point: 50 ID: 3 更新: 在第一个循环中将player_data assignemnt更改为单个表后,它的计数总是为3. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
推荐文章
站长推荐
- 从其他应用程序加载Log.txt到Memo – Delphi7
- 分享:用四十种语言分别写一个MD5算法 之6 Delph
- lua-环境配置-Sublime Text 3-windows10-lua-5-3
- groovy.lang.GroovyRuntimeException: Conflicti
- Perl类命名约定
- Golang中的runtime.LockOSThread的优点
- vb.net – VBA中的AndAlso/OrElse
- inno-setup – 如何在Inno Setup中获取完整的计算
- java – 在运行时动态添加spring上下文配置?
- 如何配置Spring为Transactional使用aspectj?
热点阅读