Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read file line by line into array

sorry i am still learning about lua. could you correct me, why the data from file wont read line by line ?

this is my example data in file points.txt :

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

so when i got from above is the 2d array(table)

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

so the problem is, when i try to loop for printing all of data in file. it just print only the lastest line. so what i wanted print all of them

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
like image 249
Han Avatar asked Nov 07 '16 08:11

Han


People also ask

How do you read a file into an array?

In Java, we can store the content of the file into an array either by reading the file using a scanner or bufferedReader or FileReader or by using readAllLines method.

How do I read a file line by line?

Java Read File line by line using BufferedReader We can use java. io. BufferedReader readLine() method to read file line by line to String. This method returns null when end of file is reached.

How do I read a text file line by line in Python?

Method 1: Read a File Line by Line using readlines() readlines() is used to read all the lines at a single go and then return them as each line a string element in a list. This function can be used for small files, as it reads the whole file content to the memory, then split it into separate lines.


1 Answers

player_data have index of 1 always, because you dont add items to it you add them to line_points which's #line_points is 5, so use it instead.

Is that what you wanted:?

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
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 in first loop to single table, it count always will be 3.

like image 168
BladeMight Avatar answered Nov 15 '22 07:11

BladeMight