Jump to content




Reading a table from a different file


  • You cannot reply to this topic
5 replies to this topic

#1 Inumel

  • Members
  • 120 posts

Posted 15 May 2013 - 04:50 AM

Hello! Elg here again ;)

I am trying, as the title says, to read a tables contents from another file!

this is the main code

http://pastebin.com/44WMZwry

and this is the table im trying to read

http://pastebin.com/R0DrG1f8

This is using the immibis core mod, using the mag card system, but i dont believe that changes the code im looking for any!


I want to use the table from the file, as data for an if statement!


Any help is extremely appreciated, as always!

#2 LordIkol

  • Members
  • 197 posts
  • LocationSwitzerland

Posted 15 May 2013 - 06:31 AM

Hi Elgriton,

The txtfile has to be altered to look like this, so remove the "passs =" stuff

{"XTF93hlQV+sf9x4aB0GOtA==","56u2g+nWTUjFsB4AFM7Lg=="}

local function loadTable(str) -- create a function where str is path and filename for the password file
local h = fs.open(tostring(str), "r") -- opens the file in read mode
local nstr = h.readAll()  -- reads the content to a string
local newtable = textutils.unserialize(nstr) -- converts the string to a table 
return newtable -- returns the table
end

passs = loadTable("pass.txt") -- use the new function to assign the table to a variable

for i =1,#passtable do -- this is just a loop to show the passwords  
print(passs[i]) -- you can access the password by index 
end

Greets Loki

#3 Inumel

  • Members
  • 120 posts

Posted 15 May 2013 - 07:25 AM

View PostLordIkol, on 15 May 2013 - 06:31 AM, said:

Hi Elgriton,

The txtfile has to be altered to look like this, so remove the "passs =" stuff

{"XTF93hlQV+sf9x4aB0GOtA==","56u2g+nWTUjFsB4AFM7Lg=="}

local function loadTable(str) -- create a function where str is path and filename for the password file
local h = fs.open(tostring(str), "r") -- opens the file in read mode
local nstr = h.readAll()  -- reads the content to a string
local newtable = textutils.unserialize(nstr) -- converts the string to a table
return newtable -- returns the table
end

passs = loadTable("pass.txt") -- use the new function to assign the table to a variable

for i =1,#passtable do -- this is just a loop to show the passwords  
print(passs[i]) -- you can access the password by index
end

Greets Loki

This confuses me a bit, I dont get any errors when i run it, and when i print the passwords it works fine, but i cant use them in an iff statement, meaning


if pass == "whatever" then
  print("works?")
else
  print("no works:(")
end

replacing "whatever" with whatever you want to compare in the table

#4 LordIkol

  • Members
  • 197 posts
  • LocationSwitzerland

Posted 15 May 2013 - 07:39 AM

you need to use pass[1] or pass[2] cause the table u where giving here has 2 values

1 = XTF93hlQV+sf9x4aB0GOtA==
2 = 56u2g+nWTUjFsB4AFM7Lg==


#5 Inumel

  • Members
  • 120 posts

Posted 15 May 2013 - 07:40 AM

View PostLordIkol, on 15 May 2013 - 07:39 AM, said:

you need to use pass[1] or pass[2] cause the table u where giving here has 2 values

1 = XTF93hlQV+sf9x4aB0GOtA==
2 = 56u2g+nWTUjFsB4AFM7Lg==


You are such a life saver!! thank you my friend! :)

#6 LordIkol

  • Members
  • 197 posts
  • LocationSwitzerland

Posted 15 May 2013 - 07:57 AM

Thank you buddy, thats what we are here for, helping each other :)





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users