I tried a mutliple of way to do it
My goal is to read a file and store line1 in a array at index 1,line2 at index 2 ect...
How to store file text into array
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
How to store file text into array
lua,contrary to other language,give you as much space as moon
Re: How to store file text into array
Looks like https://stackoverflow.com/a/11204889
Use the iterator to iterate each line in file:
Code: Select all
for line in io.lines(file) do
table.insert (lines, line)
end
Re: How to store file text into array
hi, just in case it helps have a look at the function I created for this purpose, it simulates the <br> label of html to separate lines, feel free to use it or tweak it to your needs: https://codeberg.org/glitchapp/fish-fil ... /About.lua
Re: How to store file text into array
Ok it work but it doesn't seem to left \n
So my question now is...
Exemple:
for line in love.filesystem.lines( "test/wow.txt" ) do table.insert (data, line) end
For i=1,#data[1]+1 do
If string.sub(data[1],i,i) == ? Then
Some code
End
End
...How would i detect when i is out of the string
In other words,what should I put at the ? So the condition is true when it access out of the string
So my question now is...
Exemple:
for line in love.filesystem.lines( "test/wow.txt" ) do table.insert (data, line) end
For i=1,#data[1]+1 do
If string.sub(data[1],i,i) == ? Then
Some code
End
End
...How would i detect when i is out of the string
In other words,what should I put at the ? So the condition is true when it access out of the string
lua,contrary to other language,give you as much space as moon
Re: How to store file text into array
You need to put the following where the ? is:
Code: Select all
""
Code: Select all
i == #data[1]
Code: Select all
i == #data[1]+1
Code: Select all
string.sub(data[1],i,i) == ?
If you want to keep the newline character \n at the end of each line then you can just add it before storing the line in the array:
Code: Select all
for line in io.lines(file) do
table.insert (lines, line .. "\n")
end
Who is online
Users browsing this forum: Semrush [Bot] and 4 guests