Loading a file, and writing lines from that file to a variable?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
rgbmew
Prole
Posts: 2
Joined: Sun Oct 10, 2021 1:04 am

Loading a file, and writing lines from that file to a variable?

Post by rgbmew »

Hi, I'm coming from years of using GML, so I understand the logic, but just can't find the words for what I'm trying to do.
I have a text file that basically contains a bunch of lines like this;

15011000
16011007
01013500
01020001
13022210
14022000
05030570
01038004


all of the lines are the same length, 8 characters, and are all on their own, separate lines.

All I want to do is loop through each individual line in the file, and convert each line to a new string entry in an array, but I'm having trouble understanding how file is loaded, and how to copy just 1 line to a string. I read a lot of the manual, and came across love.filesystem.lines(), but I'm uncertain how it works. The example the documentation gave was just

Code: Select all

local highscores = {}
for line in love.filesystem.lines("highscores.lst") do
  table.insert(highscores, line)
end
Which isn't the most understandable for me. How is highscores.lst formatted? What is 'line' being used in the for loop? Is this even applicable to my situation? It seems like love.filesystem.lines() is the function I'm looking for, I just don't get how it's being used in the example situation, and how it relates to my situation.

Assuming 'line' in the example is the number of lines in the file, I feel like the solution would be something like

Code: Select all

for line in love.filesystem.lines(file) do
  array[line] = (string loaded from love.filesystem.lines())
end
but I'm just not sure what '(string loaded from love.filesystem.lines())' would ACTUALLY be, if that makes sense?
Yozzaxia
Prole
Posts: 10
Joined: Fri Jul 16, 2021 3:36 pm

Re: Loading a file, and writing lines from that file to a variable?

Post by Yozzaxia »

For loops in lua can have the iteration function provide any variable type.
`line` is not the number of lines in a file, it's the line itself, in string form.

Code: Select all

 --Will print each line, in order, until the end of the file.
for line in love.filesystem.lines("highscores.lst") do
	print(line)
end
User avatar
BrotSagtMist
Party member
Posts: 636
Joined: Fri Aug 06, 2021 10:30 pm

Re: Loading a file, and writing lines from that file to a variable?

Post by BrotSagtMist »

1.There is no formating, just bytes.
2.Line is a string.
3.Yes.
Verbosely in pseudo code the construct equals:
filehandle=open("highscores.lst")
repeat
line=readline(filehandle)
highscores[linenumber]=line
until not line

So basically love.filesystem.lines will open the given file, then creates the repeat loop for you and within the for loop _line_ will turn to a string of each line the file has.
Note that you have no line number there, the position is implied by checking how big the table highscores is. We simply append the current line to the end of the table thus the table will automatically have correct line numbers. You can add a position counter yourself but that is not custom.
Another example:

Code: Select all

Text = {} --create storage table
for line in love.filesystem.lines("main.lua") do --open main.lua
 --Turn the Last entry plus one, that means create one, of table to the string _line_
 Text[#Text+1]= line 
end
for k,v in ipairs(Text) do --create a loop over the table
 print(k,v) --print each key and its value of Text
end
obey
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 2 guests