This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
SuicidalSTDz's profile picture

Iterating over all the lines in a file

Started by SuicidalSTDz, 26 February 2013 - 02:41 PM
SuicidalSTDz #1
Posted 26 February 2013 - 03:41 PM
I'm pretty darn tired and i'm sure i'm derping really hard. Here is my code:

local handle = io.open("test","r")
local line = handle:lines()
local data = {} --A table which will later store all lines collected from the file
repeat
  table.insert(data,line)
  line = handle:lines() --Will iterate over all lines in the file
until line == nil --Will continue to insert data into the table until nil
handle:close()

stringData = print(tostring(data))
print(stringData)

bannedStrings = {
".EnderOS/.userInfo",
".EnderOS/.passInfo"
}

for i = 1,2 do
  match = string.find(data,bannedStrings[i])
  if match ~= nil then
	return error("There is a banned string in here!")
  else
	return print("No banned strings detected")
  end
end

Pretty much, it is not iterating correctly and I eventually got this :)/> (Pretty proud for doing this btw)

[attachment=1035:Capture.PNG]

If anyone can point out where i'm incorrectly iterating the file lines that would be amazing.

print(stringData)
is for debugging (Forgot to take it out)
Sammich Lord #2
Posted 26 February 2013 - 03:45 PM
This is the code I use a lot of the time:

f = fs.open("/file", "r")

local line = f.readLine()
local data = {}
while line do

  table.insert(data, line) 
  line = f.readLine()
end

f.close()


for k,v in pairs(data) do
  print(v)
end
SuicidalSTDz #3
Posted 26 February 2013 - 03:47 PM
This is the code I use a lot of the time:

f = fs.open("/file", "r")

local line = f.readLine()
local data = {}
while line do

  table.insert(data, line)
  line = f.readLine()
end

f.close()


for k,v in pairs(data) do
  print(v)
end
While loop eh, never thought of that. Thanks! ;)/>
Bubba #4
Posted 26 February 2013 - 04:17 PM
Why are you iterating over every line instead of just using f.readAll() and then using the string.find() operation on that? Seams much easier for this.
SuicidalSTDz #5
Posted 26 February 2013 - 04:21 PM
Why are you iterating over every line instead of just using f.readAll() and then using the string.find() operation on that? Seams much easier for this.
Got my code working:


local lineNum = 0
f = fs.open("/test", "r")
local line = f.readLine()
local data = {}
while line do
  table.insert(data, line)
  line = f.readLine()
  lineNum = lineNum+1
end
f.close()
print(lineNum)
sleep(4)

bannedStrings = {
".EnderOS/.userInfo",
".EnderOS/.passInfo"
}

for i = 1,2 do
  match = string.find(data[lineNum],bannedStrings[i])
  if match ~= nil then
	return error("There is a banned string in here!")
  else
	return print("No banned strings detected")
  end
end
I guess I could have done it that way, but who doesn't like a challenge at 10:24 PM? ;)/>

EDIT: Also by doing it this way, I can detect how many lines are in a file (Not sure if you can the other way using f.readAll())
immibis #6
Posted 26 February 2013 - 04:44 PM
Here's a neat trick:

f = fs.open("/test", "r")
local data = {}
for line in f.readLine do
  table.insert(data, line)
end
f.close()
SuicidalSTDz #7
Posted 26 February 2013 - 06:05 PM
Here's a neat trick:

f = fs.open("/test", "r")
local data = {}
for line in f.readLine do
  table.insert(data, line)
end
f.close()
I thought I could do something like that. Thanks for confirming. Now just to test. I most likely will use f.readAll since it is obviously the superior choice.