read textfile and save in a multidimensional table
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
read textfile and save in a multidimensional table
Hi,
i,m trying to read a .txt that represents a labyrinth in the form and than put it in a table
1
1
0
1
1
1
0
this is my code, but it doesn#t really work. Can someone tell me whats the problem, that happens in conf.lua
map = {}
for line in love.filesystem.lines("labyrinth2.txt") do
for x = 1,12 do
map[x] = {}
for y = 1 , 13 do
map[x][y] = tonumber(line)
end
end
end
I first tryed to read a file in this form
11111111111111
11000111001001and so on and used table.insert, but the value ist string but that is not right
thanks
i,m trying to read a .txt that represents a labyrinth in the form and than put it in a table
1
1
0
1
1
1
0
this is my code, but it doesn#t really work. Can someone tell me whats the problem, that happens in conf.lua
map = {}
for line in love.filesystem.lines("labyrinth2.txt") do
for x = 1,12 do
map[x] = {}
for y = 1 , 13 do
map[x][y] = tonumber(line)
end
end
end
I first tryed to read a file in this form
11111111111111
11000111001001and so on and used table.insert, but the value ist string but that is not right
thanks
Re: read textfile and save in a multidimensional table
You are calling tonumber on an entire line, and you are looping over the file and building the whole table once for each line you load. That's going to give some strange results.
Instead of looping over an x and a y, you have to loop over each line, and then each character in that line.
So your final loop structure should look something like this:
I'll leave it to you to figure out how to read each character in a line. But this should help as far as basic structure goes. If there's any more questions, just ask.
Instead of looping over an x and a y, you have to loop over each line, and then each character in that line.
So your final loop structure should look something like this:
Code: Select all
map = {}
x = 1
y = 1
for line in love.filesystem.lines("labyrinth2.txt") do
map[x] = {}
for character in lineContent do
map[x][y] = character
end
x = x + 1
y = 1
end
Re: read textfile and save in a multidimensional table
It's probably better if you follow Plu's advice and iterate over a line of values and add them to the table. Here's an example that works if you want to have a value on every line. I kind of hardcoded the 13 in there, so that's not the prettiest example.
You should not do anything game logic related in conf.lua because it runs at a very early stage and the other modules are not loaded at that point. I recommend to do things like map loading from main.lua.
We have a brand new forum etiquette. Please try to follow it, especially the part with the [code][/code] tags.
Code: Select all
map = {}
for line in love.filesystem.lines("labyrinth2.txt") do
local num_tables_in_map = #map
-- Checks if there's a table or if we already reached the maximum.
if num_tables_in_map == 0 or #map[num_tables_in_map] == 13 then
-- Adds a new table for the next group of values.
table.insert(map, {})
num_tables_in_map = num_tables_in_map + 1
end
table.insert(map[num_tables_in_map], tonumber(line))
end
We have a brand new forum etiquette. Please try to follow it, especially the part with the [code][/code] tags.
Shallow indentations.
Re: read textfile and save in a multidimensional table
Hi, thank you very much and sorry that I answer so late, I didn't have much time recently.
I've tried Plu's Code, but it didn't work. I tried something else, that I thought it would work, but it still doesn't. So my file looks like this now:
1 1 1 1 0 0 0 0 0 1 1
0 0 0 1 1 1 1 0 0 0 0
and that's my code:
I don't understand why it doesn't work. Can you help me please. I hope I've hold with the Ettiquette.
Thanks!
I've tried Plu's Code, but it didn't work. I tried something else, that I thought it would work, but it still doesn't. So my file looks like this now:
1 1 1 1 0 0 0 0 0 1 1
0 0 0 1 1 1 1 0 0 0 0
and that's my code:
Code: Select all
tabstring = {}
for line in love.filesystem.lines("assets/labyrinth3.txt") do
table.insert(tabstring, line)
end
map ={}
for k = 1, table.getn(tabstring) do
map[k]={}
for v,i in string.gmatch(tabstring[k], "%S+") do
map[k][v] = i -- or tonumber(i)??
end
end
I don't understand why it doesn't work. Can you help me please. I hope I've hold with the Ettiquette.
Thanks!
-
- Party member
- Posts: 712
- Joined: Fri Jun 22, 2012 4:54 pm
- Contact:
Re: read textfile and save in a multidimensional table
Hi,
You should stick to Plu's code, it does what you want to do. However, he's only written some pseudo-code for the lines:
... this is not proper Lua, he just wanted to push you in the right direction, so you can work from his example and figure out how to write those lines in Lua.
You could try replacing those three lines he mentioned by something like (use the rest of his code as it is):
You should stick to Plu's code, it does what you want to do. However, he's only written some pseudo-code for the lines:
Code: Select all
for character in lineContent do
map[x][y] = character
end
You could try replacing those three lines he mentioned by something like (use the rest of his code as it is):
Code: Select all
-- go through all characters in the line:
while y <= #line do
-- get y'th character from line and save it into map[x][y]
map[x][y] = line:sub(y, y)
y = y + 1 --- continue with the next character
end
trAInsported - Write AI to control your trains
Bandana (Dev blog) - Platformer featuring an awesome little ninja by Micha and me
GridCars - Our jam entry for LD31
Germanunkol.de
Bandana (Dev blog) - Platformer featuring an awesome little ninja by Micha and me
GridCars - Our jam entry for LD31
Germanunkol.de
Re: read textfile and save in a multidimensional table
Hi,
thanks for the answer. I've tried with the code last posted. but I can still not load the txt file and I need it so much. that's my code please tell me what is wrong, I can't find the mistake.
and my txt file looks like this:
0 0 1 1 0 0 0 0 0 0 0 1 0 0 0 0
0 0 0 0 0 0 1 1 0 0 0 1 1 1 1 1
0 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0
I load my project so you can see the whole code. but It doesnot work when I add my code above. I'll be very thankful if someone tell me where the ptroblem is .
Regards
thanks for the answer. I've tried with the code last posted. but I can still not load the txt file and I need it so much. that's my code please tell me what is wrong, I can't find the mistake.
Code: Select all
tabstring = {}
for line in love.filesystem.lines("assets/labyrinth3.txt") do
table.insert(tabstring, line)
end
map ={}
v=1
for k = 1, table.getn(tabstring) do
map[k]={}
for i in string.gmatch(tabstring[k], "%a+") do
map[k][v] = tonumber(i)
v= v+1
end
end
0 0 1 1 0 0 0 0 0 0 0 1 0 0 0 0
0 0 0 0 0 0 1 1 0 0 0 1 1 1 1 1
0 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0
I load my project so you can see the whole code. but It doesnot work when I add my code above. I'll be very thankful if someone tell me where the ptroblem is .
Regards
Last edited by nadula on Sun Oct 13, 2013 8:42 pm, edited 1 time in total.
Re: read textfile and save in a multidimensional table
First of all, we need the whole file. Your error might be regarding a misspelling of some sort, so this can help us out greatly.
Do you know any of the string. commands? That might help you.
Here are the steps in plain English. If you still need any help, I will push you in the right direction: (note that you don't have to do this, this is just what I'd do)
Make a table.
Make a for-loop that goes through every line in the file.
Save each line as a local variable (called, for example, line). Look on the wiki if you need to learn how to do that (or use a library, like mine (admittedly, self advertising, but whatever...)).
Make a new table in the old table.
Get the length of that line and store it as a local variable (length or whatever).
Make a for loop to iterate until the length of that line.
Get only one of the characters in that line of text, and insert it into the newer table.
Repeat until over.
Hope this helped!
Do you know any of the string. commands? That might help you.
Here are the steps in plain English. If you still need any help, I will push you in the right direction: (note that you don't have to do this, this is just what I'd do)
Make a table.
Make a for-loop that goes through every line in the file.
Save each line as a local variable (called, for example, line). Look on the wiki if you need to learn how to do that (or use a library, like mine (admittedly, self advertising, but whatever...)).
Make a new table in the old table.
Get the length of that line and store it as a local variable (length or whatever).
Make a for loop to iterate until the length of that line.
Get only one of the characters in that line of text, and insert it into the newer table.
Repeat until over.
Hope this helped!
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
Re: read textfile and save in a multidimensional table
Hi,
these are the files, there also asstets but I can't post them,because of the limit of files. The txt is that:
0 0 1 1 0 0 0 0 0 0 0 1 0 0 0 0
0 0 0 0 0 0 1 1 0 0 0 1 1 1 1 1
0 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0
0 0 1 1 1 1 1 1 1 1 0 1 1 1 1 0
0 0 1 0 0 0 0 0 0 0 0 1 1 0 0 0
1 1 1 0 0 1 1 1 1 1 1 0 1 0 0 0
0 0 1 0 0 0 0 0 0 0 1 0 1 0 0 0
0 0 1 1 1 1 0 0 0 0 1 0 1 1 1 1
0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0
0 0 1 0 0 0 1 0 0 0 1 0 1 1 1 0
1 1 1 0 0 0 1 0 0 0 0 0 1 0 1 0
0 0 1 1 1 1 1 0 0 0 1 1 1 0 1 0
0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0
0 0 0 0 0 0 1 1 1 1 1 1 0 0 1 0
0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0
0 0 0 0 0 0 0 1 1 1 1 1 0 0 1 0
thank you
these are the files, there also asstets but I can't post them,because of the limit of files. The txt is that:
0 0 1 1 0 0 0 0 0 0 0 1 0 0 0 0
0 0 0 0 0 0 1 1 0 0 0 1 1 1 1 1
0 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0
0 0 1 1 1 1 1 1 1 1 0 1 1 1 1 0
0 0 1 0 0 0 0 0 0 0 0 1 1 0 0 0
1 1 1 0 0 1 1 1 1 1 1 0 1 0 0 0
0 0 1 0 0 0 0 0 0 0 1 0 1 0 0 0
0 0 1 1 1 1 0 0 0 0 1 0 1 1 1 1
0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0
0 0 1 0 0 0 1 0 0 0 1 0 1 1 1 0
1 1 1 0 0 0 1 0 0 0 0 0 1 0 1 0
0 0 1 1 1 1 1 0 0 0 1 1 1 0 1 0
0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0
0 0 0 0 0 0 1 1 1 1 1 1 0 0 1 0
0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0
0 0 0 0 0 0 0 1 1 1 1 1 0 0 1 0
thank you
Re: read textfile and save in a multidimensional table
Code: Select all
local function explode(str,div)
local result = {}
local pos = 0
while (true) do
local ps,pe = string.find(str,div,pos,true)
if (ps == nil) then
if (#str > 0) then
result[#result+1] = string.sub(str,pos)
end
break
end
result[#result+1] = string.sub(str,pos,ps-1)
pos = pe+1
end
return result
end
local function processFile(filename)
local contents = love.filesystem.read(filename)
local lines = explode(contents,"\n")
for y=1,#lines do
local line = explode(lines[y]," ")
for x=1,#line do
io.write(line[x])
end
io.write("\n")
end
end
Re: read textfile and save in a multidimensional table
Just if your are still stuck.
Another approach is to use a string splitting function - simple and works.
For what is worth.
Best
Edit: I guess it's not worth anything as I cant attach.
Edit 2: Hear it is.
Another approach is to use a string splitting function - simple and works.
Code: Select all
--------------------------------------------------
-- Break up fields separated by 'sep' character --
--------------------------------------------------
function string:split( sep ) -- convert string to table of strings
local sep, tab = sep or ',', {}
local pattern = string.format('([^%s]+)', sep )
self:gsub( pattern, function(c) tab[ #tab+1 ] = c end )
return tab
end
Best
Edit: I guess it's not worth anything as I cant attach.
Edit 2: Hear it is.
- Attachments
-
- DataFiles.love
- Your data
- (1.06 KiB) Downloaded 313 times
Who is online
Users browsing this forum: Bing [Bot], Brotein and 3 guests