Jump to content
  • Sky
  • Blueberry
  • Slate
  • Blackcurrant
  • Watermelon
  • Strawberry
  • Orange
  • Banana
  • Apple
  • Emerald
  • Chocolate
  • Charcoal
  • 0
Karasuro

io.read string to table

Question

So I have tables in a text file. 1 line per table so that when I read from the file using var:lines() and put each line into a table in the program, they are accessible. However, now I have a table of strings that looks like this:

"{user = "ADMIN", pass = "ADMIN", settings = {}, admin = true},"
"{user = "KARASURO", pass = "TEST", settings = {} }"

What would be the best way to get these tables from the file without them being strings?

(How does one table a string?)

Link to post
Share on other sites

5 answers to this question

Recommended Posts

  • 0

So, I guess, you want to unserialize a table. This can be done using the unserialize function from the serialization library. Here's the sample code:

-- Require the library
local srl = require("serialization")

-- The table of serialized tables
tables = {
  '{user = "ADMIN", pass = "ADMIN", settings = {}, admin = true}',
  '{user = "KARASURO", pass = "TEST", settings = {}}'
}

-- The table where unserialized tables will be put
unsrlTables = {}

-- Finally, unserialize
for k, tbl in pairs(tables) do
  unsrlTables[k] = srl.unserialize(tbl)
end
Link to post
Share on other sites
  • 0

I'm only receiving the last line from the file in my table so there's only 1 entry?

What code are you using to unserialize the file? (if you had said it was the first line you might have gotten around it by making the file contain one table instead of two (wrap the entire thing in {}))

 

Maybe you need to remove the trailing comma from the lines - every line except the last one has a comma at the end, and a trailing comma isn't valid table/serialization syntax, that would explain why only the last line loads.

Actually, from the lua manual (section 3.4.8 or 3.4.9 - Table Constructors) "The field list can have an optional trailing separator, as a convenience for machine-generated code."

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use and Privacy Policy.