Lua – reading documentation to an array

Sorry, I am still learning Lua. You can correct me, why the data in the file is not read line by line?

This is my sample data in the file points.txt:

lexxo:30:1
rey:40: 2
lion:40:2
prince:50:3
royal:50:3

So when I get from the above is a 2d array (table)

player = {{(name),(points),(which var point earned on index)},
{(...),(...) ,(...)));

So the problem is that when I try to print all the data in the file in a loop. It only prints the latest line. So I want to print all of them

< p>

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, playerpoint, 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 always has index 1, because you did not add items to it, you added them to line_points, where #line_points is 5, so please use it instead.

That Is it what you want?

line_points = {}
player_data = {{}} --I think you can delete it at all...
--Because it is rewriting each time.

local rfile = io.open("points.txt", "r")
for line in rfile:lines() do
playername, playerpoint, playeridpoint = line:match("^(.-):(%d+):(%d+)$")
player_data = {playername, playerpoint, 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< br />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_poin ts[i][3]);
end

Output:

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

Update:

After changing player_data assignemnt to a single table in the first loop, its count is always 3.

Sorry, I am still learning Lua. You can correct me, why the data in the file is not read line by line?

This is my sample data in the file points.txt:

lexxo:30:1
rey:40: 2
lion:40:2
prince:50:3
royal:50:3

So when I get from the above is a 2d array (table)

player = {{(name),(points),(which var point earned on index)},
{(...),(...) ,(...)));

So the problem is that when I try to print all the data in the file in a loop. It only prints the latest line. So I want to print all of them

< p>

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, playerpoint, 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 always has index 1, because you did not add to it Project, you add them to line_points, where #line_points is 5, so please use it instead.

Is that what you want?

line_points = {}
player_data = {{}} --I think you can delete it at all...
--Because it is rewriting each time.

local rfile = io.open("points.txt", "r")
for line in rfile:lines() do
playername, playerpoint, playeridpoint = line:match("^(.-):(%d+):(%d+)$")
player_data = {playername, playerpoint, 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< br />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

Output:

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

Update:

After changing player_data assignemnt to a single table in the first loop, its count is always 3.

Leave a Comment

Your email address will not be published.