Jump to content




Iterating over all the lines in a file


6 replies to this topic

#1 SuicidalSTDz

    Permutator of Strings

  • Members
  • 1,308 posts
  • LocationPennsylvania

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)

Attached Image: 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)

#2 Sammich Lord

    IRC Addict

  • Members
  • 1,212 posts
  • LocationThe Sammich Kingdom

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


#3 SuicidalSTDz

    Permutator of Strings

  • Members
  • 1,308 posts
  • LocationPennsylvania

Posted 26 February 2013 - 03:47 PM

View PostSammich Lord, on 26 February 2013 - 03:45 PM, said:

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! ;)

#4 Bubba

    Use Code Tags!

  • Moderators
  • 1,142 posts
  • LocationRHIT

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.

#5 SuicidalSTDz

    Permutator of Strings

  • Members
  • 1,308 posts
  • LocationPennsylvania

Posted 26 February 2013 - 04:21 PM

View PostBubba, on 26 February 2013 - 04:17 PM, said:

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

#6 immibis

    Lua God

  • Members
  • 1,033 posts
  • LocationWellington, New Zealand

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


#7 SuicidalSTDz

    Permutator of Strings

  • Members
  • 1,308 posts
  • LocationPennsylvania

Posted 26 February 2013 - 06:05 PM

View Postimmibis, on 26 February 2013 - 04:44 PM, said:

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.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users