Page 1 of 2

read textfile and save in a multidimensional table

Posted: Wed Sep 25, 2013 12:52 pm
by nadula
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

Re: read textfile and save in a multidimensional table

Posted: Wed Sep 25, 2013 1:32 pm
by Plu
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:

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
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.

Re: read textfile and save in a multidimensional table

Posted: Wed Sep 25, 2013 1:46 pm
by Boolsheet
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.

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
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.

Re: read textfile and save in a multidimensional table

Posted: Tue Oct 08, 2013 8:16 pm
by nadula
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:

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!

Re: read textfile and save in a multidimensional table

Posted: Wed Oct 09, 2013 8:53 am
by Germanunkol
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:

Code: Select all

  for character in lineContent do
    map[x][y] = character
  end
... 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):

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

Re: read textfile and save in a multidimensional table

Posted: Sun Oct 13, 2013 3:41 pm
by nadula
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.

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
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

Re: read textfile and save in a multidimensional table

Posted: Sun Oct 13, 2013 5:12 pm
by davisdude
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! :)

Re: read textfile and save in a multidimensional table

Posted: Sun Oct 13, 2013 8:51 pm
by nadula
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

Re: read textfile and save in a multidimensional table

Posted: Mon Oct 14, 2013 6:25 am
by Azhukar

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

Posted: Mon Oct 14, 2013 10:18 pm
by Ref
Just if your are still stuck.
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
For what is worth.
Best
Edit: I guess it's not worth anything as I cant attach.
Edit 2: Hear it is.