Page 1 of 1

return numbers from a log (.txt)

Posted: Sun Mar 02, 2014 8:15 am
by ZZZZZ
I have been trying to read a log file in order to get the numbers from it. The list in the log file looks something like this:

Code: Select all

	Slot	
	71
	72
	74
	75
	76
	77
Tried using this function:

Code: Select all

function checkSlots()
		local lines = {}
		local filename = getExecutionPath() .. "/logs/"..player.Name..".txt"
		local file, err = io.open(filename, "r");
		if( not file ) then
			print("File not found");
		else
			-- read the lines in table 'lines'
			for line in io.lines(filename) do
				table.insert(lines, line)
			end 
			
			table.concat(lines, "\n")
			for k,v in pairs(lines) do
				local number = file:read(k, "*n")
				print(number);
			end
			file:close()
		end
	end
but it returns

Code: Select all

Command> checkSlots()

Sl
ot

        72

        75


        77
nil
I am trying to do something like

Code: Select all

for k,v in pairs(lines) do
				local number = file:read(k, "*n")
				if type(number) == "number" then
					item = inventory.BagSlot[number]
					 if not (item.Id == 208679 or item.Id == 208682 or item.Id == 0) and item.Quality < 3 then
						print(item.Name)
					end
				end
			end
Basically trying to get the number's in the list into the variable number. Keeps coming up with nil value.

Re: return numbers from a log (.txt)

Posted: Sun Mar 02, 2014 10:46 am
by rock5
Let me see

Code: Select all

function checkSlots()
      local lines = {}
      local filename = getExecutionPath() .. "/logs/"..player.Name..".txt"
      local file, err = io.open(filename, "r");
      if( not file ) then
         print("File not found");
      else
         -- read the lines in table 'lines'
         for line in io.lines(filename) do
            table.insert(lines, line)
         end 
At this point you have a table with the whole contents of the file. At this point you could close the file because you don't need it anymore.

Code: Select all

         table.concat(lines, "\n")
This line does nothing. It concatenates the table but does nothing with the result. 'lines' remains unchanged.

Code: Select all

         for k,v in pairs(lines) do
            local number = file:read(k, "*n")
            print(number);
         end
This doesn't make sense. Why are you rereading the file? It looks like you think that the first argument of read is the line to be read but from online references it seems to be the number of bytes read.

What I would do is use the existing line values and use tonumber on them.

Code: Select all

function checkSlots()
		local lines = {}
		local filename = getExecutionPath() .. "/logs/"..player.Name..".txt"
		local file, err = io.open(filename, "r");
		if( not file ) then
			print("File not found");
		else
			-- read the lines in table 'lines'
			for line in io.lines(filename) do
				table.insert(lines, tonumber(line))
			end
			file:close()

			for k,v in pairs(lines) do
				item = inventory.BagSlot[v]
                if not (item.Id == 208679 or item.Id == 208682 or item.Id == 0) and item.Quality < 3 then
					print(item.Name)
				end
			end
		end
	end
This seems to work.

Re: return numbers from a log (.txt)

Posted: Sun Mar 02, 2014 7:04 pm
by ZZZZZ
Thank you, works :) First time trying to figure out how to read log files, hence the random pointless crap xD