handling data files

Hi, I’m new and am fumbling my way through Lua and Corona to try and build an app. I’m using the learning by doing approach, with help from a book I bought and all the guides and tutorials I found on here.

Anyway, my problem is data arrays [that name may be incorrect though it’s how I would have thought of them when I actually used to code back in the 80s/90s].

What I’m trying to do is load (and later save) a file of character information (for a role play encounter manager). So the file would contain something like:

Fred, Human, 12, false, 4

Jane, Elf, 18, true, 3

Bert, Human, 17, 3

I’m presuming from what I’ve read that I would load this in to a table ( declared by something like charTable = {} )

and then have a row per character ( charTable[i] ) and within that the details such as charTable[i].Name, charTable[i].Race.

First off, I think that kind of thing is right, though would appreciate someone saying yes or no on that.

Now, I’ve create a text file with this stuff and I’m opening it fine, using the

for line in file:lines() do

end

approach from the documentation on here.

My problem (after all this typing) is that this reads in the whole line and I want to split it into the separate items. But I can’t see anything that helps me find out how to do that.

Can anyone help me with this (I’m cool with a simple “go read this …”) and also, am I going about this the wrong way completely?

Thanks in advance for any help

Guy

Hello Guy and welcome to the Corona Labs forums!

Lua has very few data types:  Number, String, Boolean, Function, Userdata and Table (there may be a few more, but that’s the core).

In Lua tables serve the purpose of many different data tables from arrays to object-oriented like objects. Lua tables can either be indexed by strings or by numbers. When indexed by numbers, these are very much like array’s in other languages. Unlike C based languages and more like BASIC, Lua’s numeric indexed tables start at 1, not 0.  When a table is indexed by a string or a “key”, it becomes more of a key-value representation. In PHP this would be an Associative Array. In Perl, it would be a hash table.

Declare a table:

local myTable = {}

Add some entries:

myTable[1] = 10 myTable[2] = 15

etc. This can be short cut to:

local myTable = { 10, 15 }

That line of code will both define it as a table and populate each item with the values listed.  A key-value table is more like:

local myTable = {} myTable.strength = 18 myTable.dexterity = 12 myTable["wisdom"] = 10

Note you can either use the dot (.) syntax or the bracket ([""]) syntax. Both are identical with one exception. If your key is has anything other than characters and numbers, you have to use the bracket syntax. This could be short cut to be:

local myTable = { strength = 18, dexterity = 12, ["wisdom"] = 10 }

The values can be anything, numbers, strings, function or other tables. Lua doesn’t have multi-dimensional arrays, so you end up with a table of tables to accomplish it.  It’s common to see something like a numerically indexed table that has key indexed tables as it’s values:

local myTable = {} myTable[1] = { strength = 18, dexterity = 12, wisdom = 10 } myTable[2] = { strength = 12, dexterity = 6, wisdom = 18 }

or shortcut to:

local myTable = {      { strength = 18, dexterity = 12, wisdom = 10 },      { strength = 12, dexterity = 6, wisdom = 18 }, }

Note, its common practice to include a comma after the last entry so that if you add another entry, you won’t forget to add the comma!

Finally as far as loading the data from a file, Corona certainly can read in a line and then you can use various string.* functions to parse the string into values. But we also have JSON support through the json.* API calls. JSON is a pretty standard way of representing data in text. If you can produce a JSON file instead of your command delimited file, you can parse it with one line of code. Read the whole file in one big block of text and feed that to json.decode() and get a table of data out of it.  But your format, as long as there are no comma’s in the description, will work for you.

Rob

Thanks for the swift response.
I’d been hacking the code from your string magic article though now I think I need to go back and work from a json perspective by the sound of it.
I presume that the easy route would be for me to create an example table in Lua, the use the json API to encode it to give me a working example to try loading in.

Cheers
Guy

That would work!

JSON is definitely the way forward here.  Read/Write with JSON is easy if you load the correct library

local json = require("json")

and then use the following to write data

 local file = io.open( path, "w" ) if file then file:write( json.decode( myTable)) io.close( file ) end

and to read it

 local file = io.open( path, "r" ) if file then local contents = file:read( "\*a" ) io.close( file ) local myTable = json.decode( contents ) end

Thanks Adrian, going to copy and paste that to use.

cheers

Guy

As a final comment I thought I’d drop in here what I’ve tweaked the information you’ve given me to …

local file, errorString = io.open( path, “r” )
if not file then
    print( "File error: " … errorString )
else
    playerChars = json.decode( file:read( “*a” ) )
    io.close( file )
end

Again, thanks for the help.

Hello Guy and welcome to the Corona Labs forums!

Lua has very few data types:  Number, String, Boolean, Function, Userdata and Table (there may be a few more, but that’s the core).

In Lua tables serve the purpose of many different data tables from arrays to object-oriented like objects. Lua tables can either be indexed by strings or by numbers. When indexed by numbers, these are very much like array’s in other languages. Unlike C based languages and more like BASIC, Lua’s numeric indexed tables start at 1, not 0.  When a table is indexed by a string or a “key”, it becomes more of a key-value representation. In PHP this would be an Associative Array. In Perl, it would be a hash table.

Declare a table:

local myTable = {}

Add some entries:

myTable[1] = 10 myTable[2] = 15

etc. This can be short cut to:

local myTable = { 10, 15 }

That line of code will both define it as a table and populate each item with the values listed.  A key-value table is more like:

local myTable = {} myTable.strength = 18 myTable.dexterity = 12 myTable["wisdom"] = 10

Note you can either use the dot (.) syntax or the bracket ([""]) syntax. Both are identical with one exception. If your key is has anything other than characters and numbers, you have to use the bracket syntax. This could be short cut to be:

local myTable = { strength = 18, dexterity = 12, ["wisdom"] = 10 }

The values can be anything, numbers, strings, function or other tables. Lua doesn’t have multi-dimensional arrays, so you end up with a table of tables to accomplish it.  It’s common to see something like a numerically indexed table that has key indexed tables as it’s values:

local myTable = {} myTable[1] = { strength = 18, dexterity = 12, wisdom = 10 } myTable[2] = { strength = 12, dexterity = 6, wisdom = 18 }

or shortcut to:

local myTable = {      { strength = 18, dexterity = 12, wisdom = 10 },      { strength = 12, dexterity = 6, wisdom = 18 }, }

Note, its common practice to include a comma after the last entry so that if you add another entry, you won’t forget to add the comma!

Finally as far as loading the data from a file, Corona certainly can read in a line and then you can use various string.* functions to parse the string into values. But we also have JSON support through the json.* API calls. JSON is a pretty standard way of representing data in text. If you can produce a JSON file instead of your command delimited file, you can parse it with one line of code. Read the whole file in one big block of text and feed that to json.decode() and get a table of data out of it.  But your format, as long as there are no comma’s in the description, will work for you.

Rob

Thanks for the swift response.
I’d been hacking the code from your string magic article though now I think I need to go back and work from a json perspective by the sound of it.
I presume that the easy route would be for me to create an example table in Lua, the use the json API to encode it to give me a working example to try loading in.

Cheers
Guy

That would work!

JSON is definitely the way forward here.  Read/Write with JSON is easy if you load the correct library

local json = require("json")

and then use the following to write data

 local file = io.open( path, "w" ) if file then file:write( json.decode( myTable)) io.close( file ) end

and to read it

 local file = io.open( path, "r" ) if file then local contents = file:read( "\*a" ) io.close( file ) local myTable = json.decode( contents ) end

Thanks Adrian, going to copy and paste that to use.

cheers

Guy

As a final comment I thought I’d drop in here what I’ve tweaked the information you’ve given me to …

local file, errorString = io.open( path, “r” )
if not file then
    print( "File error: " … errorString )
else
    playerChars = json.decode( file:read( “*a” ) )
    io.close( file )
end

Again, thanks for the help.