This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
FuuuAInfiniteLoop(F.A.I.L)'s profile picture

got nil

Started by FuuuAInfiniteLoop(F.A.I.L), 16 January 2013 - 01:16 AM
FuuuAInfiniteLoop(F.A.I.L) #1
Posted 16 January 2013 - 02:16 AM

--dbapi
local function reset()
pass = nil
object = nil
output = nil
end
function decodedbtofileswithallnames(file)
file = fs.open("file", "r")
a = file.readLine()
repeat
  c = decrypt(a, "thepasswordshoulnotbeenknow")
  b = textutils.unserialize(c)
  o = fs.open(a[1], "w")
  o.write(a[2])
  a = file.readLine
  o.close()
until a==nil
end
local function encode(text)
output=""
repeat
  output=output..string.byte(text).." "
  text=string.sub(text,2)
until string.len(text)==0
end
local function encrypt(text,pass)
text = text.."\n"..pass
encode(pass)
pass=output
temppass=output
encode(text)
text=output
output=""
repeat
  a = string.find(temppass..""," ")
  b = string.sub(temppass,1,a-1)
  c = string.find(text..""," ")
  d = string.sub(text,1,c-1)
  output=output..b*d.." "
  temppass=string.sub(temppass,a+1)
  text = string.sub(text,c+1)
  if string.find(temppass," ")==nil then
   temppass=pass
  end
until string.find(text," ")==nil
end
local function decrypt(text, rawpass)
encode(rawpass)
pass=output
temppass=output
output=""
repeat
  a = string.find(text..""," ")
  b = string.sub(text,1,a-1)
  c = string.find(temppass..""," ")
  d = string.sub(temppass,1,c-1)
  test = pcall(string.char,b/d)
  if test==false then
   print("Password incorrect could not decrypt!!!")
   output=nil
   return
  end
  output = output.. string.char(b/d)
  text=string.sub(text,a+1)
  temppass=string.sub(temppass,c+1)
  if string.match(temppass," ")==nil then
   temppass=pass
  end
until string.find(text," ")==nil
if string.match(output,rawpass)==nil then
  print("Password incorrect could not decrypt!!!")
  output=nil
  return
end
output=string.gsub(output,"\n"..rawpass,"")
end
local function fencrypt(object,pass)
file=io.open(object,"r")
if file==nil then
  print(object.." does not exists")
  reset()
end
text=""
repeat
  a = file:read()
  if a~=nil then
   text=text..a.."\n"
  end
until a==nil
file:close()
encrypt(text,pass)
return output
end
local function fdecrypt(object,pass,save)
file=io.open(object,"r")
text=""
repeat
  a=file:read()
  if a~=nil then
   text=text..a.."\n"
  end
until a==nil
file:close()
decrypt(text,pass)
if output==nil then return end
if save==true then
  file=io.open(object,"w")
  file:write(output)
  file:close()
else
  return output
end
end
function setVal(key, value)
   data[key] = value
end
function getVal(key)
return data[key]
end
function gettable()
return data
end
function getData()
return object, data.autor
end
function setData(bautor)
n, a = getData()
if not a then
  data["autor"] = bautor
  return true
else
  s = shell.getRunningProgram()
  a = shell.getRunningProgram()
  b = shell.resolveProgram(a)
  print(b..": Error, db "..object.." has alredy an autor, "..data.autor)
  return false
end
end
function loadExcel(object)
a = fdecrypt(object,themegaexcelcorporationbymicroshitooohisaidexcelimeanudbbyurielsalisxD,false)
reset()
data = textutils.unserialize(a)
end
function save(name)
s = textutils.serialize(data)
a = fencrypt(s,themegaexcelcorporationbymicroshitooohisaidexcelimeanudbbyurielsalisxD)
reset()
file = fs.open(name, "w")
file.write(a)
file.close()
end
function closeExcel()
data = nil
end
When i try to create the table data to simulate a loaded db and i tried to write a val it got nil the same when i tried to load an db, i ccreated a script that load the api and then try to do that but is not working.
theoriginalbit #2
Posted 16 January 2013 - 03:15 AM
this

themegaexcelcorporationbymicroshitooohisaidexcelimeanudbbyurielsalisxD
should be in quotes as there is nowhere its declared as a variable.


this

repeat
  a=file:read()
  if a~=nil then
   text=text..a.."\n"
  end
until a==nil

can be replaced with

text = file:read("*a")

this reads all preserving new lines.

other than that, please expand on "its not working" … what exactly is not working? does it error, or is it just not performing as expected?
Edited on 16 January 2013 - 02:20 AM
FuuuAInfiniteLoop(F.A.I.L) #3
Posted 16 January 2013 - 03:53 AM
when i trie with your correctio it said: test:128 attempt to index ?, a nil value HELP
theoriginalbit #4
Posted 16 January 2013 - 03:58 AM
according to the line numbers in my ide line 128 is this

b = shell.resolveProgram(a)
what is line 128 for you?
FuuuAInfiniteLoop(F.A.I.L) #5
Posted 16 January 2013 - 04:38 AM
according to the line numbers in my ide line 128 is this

b = shell.resolveProgram(a)
what is line 128 for you?
return object, data.autor

is in the function getData()
theoriginalbit #6
Posted 16 January 2013 - 04:40 AM
Typo maybe? Shouldn't it be author not autor?

Edit: wait I do see you initialize it as that… But do you ever tell the program that data is a table? Can't see it in the code on my phone…
FuuuAInfiniteLoop(F.A.I.L) #7
Posted 16 January 2013 - 04:51 AM
Typo maybe? Shouldn't it be author not autor?

Edit: wait I do see you initialize it as that… But do you ever tell the program that data is a table? Can't see it in the code on my phone…
iim spanish so i put autor (typo) but all references are like that
theoriginalbit #8
Posted 16 January 2013 - 04:53 AM
Typo maybe? Shouldn't it be author not autor?

Edit: wait I do see you initialize it as that… But do you ever tell the program that data is a table? Can't see it in the code on my phone…
iim spanish so i put autor (typo) but all references are like that
Yeh I realized that after I said it. Put data = { } at the top… Should fix it…
FuuuAInfiniteLoop(F.A.I.L) #9
Posted 16 January 2013 - 05:11 AM

--dbapi
data = {}
local function reset()
pass = nil
object = nil
output = nil
end
function decodedbtofileswithallnames(file)
file = fs.open("file", "r")
a = file.readLine()
repeat
  c = decrypt(a, "thepasswordshoulnotbeenknow")
  b = textutils.unserialize(c)
  o = fs.open(a[1], "w")
  o.write(a[2])
  a = file.readLine
  o.close()
until a==nil
end
local function encode(text)
output=""
repeat
  output=output..string.byte(text).." "
  text=string.sub(text,2)
until string.len(text)==0
endlocal function encrypt(text,pass)
text = text.."\n"..pass
encode(pass)
pass=output
temppass=output
encode(text)
text=output
output=""
repeat
  a = string.find(temppass..""," ")
  b = string.sub(temppass,1,a-1)
  c = string.find(text..""," ")
  d = string.sub(text,1,c-1)
  output=output..b*d.." "
  temppass=string.sub(temppass,a+1)
  text = string.sub(text,c+1)
  if string.find(temppass," ")==nil then
   temppass=pass
  end
until string.find(text," ")==nil
end
local function decrypt(text, rawpass)
output = encode(rawpass)
pass=output
temppass=output
output=""
repeat
  a = string.find(text..""," ")
  b = string.sub(text,1,a-1)
  c = string.find(temppass..""," ")
  d = string.sub(temppass,1,c-1)
  test = pcall(string.char,b/d)
  if test==false then
   print("Password incorrect could not decrypt!!!")
   output=nil
   return
  end
  output = output.. string.char(b/d)
  text=string.sub(text,a+1)
  temppass=string.sub(temppass,c+1)
  if string.match(temppass," ")==nil then
   temppass=pass
  end
until string.find(text," ")==nil
if string.match(output,rawpass)==nil then
  print("Password incorrect could not decrypt!!!")
  output=nil
  return
end
output=string.gsub(output,"\n"..rawpass,"")
endlocal function fencrypt(object,pass)
file=io.open(object,"r")
if file==nil then
  print(object.." does not exists")
  reset()
end
text=""
repeat
  a = file:read()
  if a~=nil then
   text=text..a.."\n"
  end
until a==nil
file:close()
encrypt(text,pass)
return output
end
local function fdecrypt(object,pass,save)
file=io.open(object,"r")
text=""
repeat
  a=file:read()
  if a~=nil then
   text=text..a.."\n"
  end
until a==nil
file:close()
decrypt(text,pass)
if output==nil then return end
if save==true then
  file=io.open(object,"w")
  file:write(output)
  file:close()
else
  return output
end
end
function setVal(key, value)
   data[key] = value
end
function getVal(key)
return data[key]
end
function gettable()
return data
end
function getData()
return object, data["autor"]
end
function setData(bautor)
n, a = getData()
if not a then
  data["autor"] = bautor
  return true
else
  s = shell.getRunningProgram()
  a = shell.getRunningProgram()
  b = shell.resolveProgram(a)
  print(b..": Error, db "..object.." has alredy an autor, "..data.autor)
  return false
end
end
function loadExcel(object)
a = fdecrypt(object,"themegaexcelcorporationbymicroshitooohisaidexcelimeanudbbyurielsalisxD",false)
reset()
data = textutils.unserialize(a)
end
function save(name)
s = textutils.serialize(data)
a = fencrypt(s,"themegaexcelcorporationbymicroshitooohisaidexcelimeanudbbyurielsalisxD")
reset()
file = fs.open(name, "w")
file.write(a)
file.close()
end
function closeExcel()
data = nil
end
now nil is on line 52
ChunLing #10
Posted 16 January 2013 - 05:35 AM
Where is the encode() function supposed to end, and what is it supposed to return?
FuuuAInfiniteLoop(F.A.I.L) #11
Posted 16 January 2013 - 05:58 AM
Where is the encode() function supposed to end, and what is it supposed to return?

local function encode(text)
output=""
repeat
  output=output..string.byte(text).." "
  text=string.sub(text,2)
until string.len(text)==0
endlocal function encrypt(text,pass)
text = text.."\n"..pass
encode(pass)
pass=output
temppass=output
encode(text)
text=output
output=""
repeat
  a = string.find(temppass..""," ")
  b = string.sub(temppass,1,a-1)
  c = string.find(text..""," ")
  d = string.sub(text,1,c-1)
  output=output..b*d.." "
  temppass=string.sub(temppass,a+1)
  text = string.sub(text,c+1)
  if string.find(temppass," ")==nil then
   temppass=pass
  end
until string.find(text," ")==nil
end
ChunLing #12
Posted 16 January 2013 - 06:03 AM
You've managed to obscure one of your "end"s by concatenating it with a local.
endlocal function encrypt(text,pass) 
This causes encode() to still have undefined behavior when you call it.
FuuuAInfiniteLoop(F.A.I.L) #13
Posted 16 January 2013 - 06:11 AM
can you correct my code because i dont have understood anything that you said
remiX #14
Posted 16 January 2013 - 07:09 AM
Here, read the comments..

local function encode(text)
	output=""
	repeat
		output=output..string.byte(text).." "
		text=string.sub(text,2)
	until string.len(text)==0
	--[[ Your problem was here, you had
		'endlocal' as one word, instead
		of two
	endlocal function encrypt(text,pass)]]
end

local function encrypt(text,pass)
	text = text.."\n"..pass
	encode(pass)
	pass=output
	temppass=output
	encode(text)
	text=output
	output=""
	repeat
		a = string.find(temppass..""," ")
		b = string.sub(temppass,1,a-1)
		c = string.find(text..""," ")
		d = string.sub(text,1,c-1)
		output=output..b*d.." "
		temppass=string.sub(temppass,a+1)
		text = string.sub(text,c+1)
		if string.find(temppass," ")==nil then
			temppass=pass
		end
	until string.find(text," ")==nil
end