How to read text file and add lines to an array?

Hello,

I have a file which is full of questions and answers. The way it is formatted is a question line and then the next 4 lines are possible answers. The correct answer has an * in the text. How can i take this text file that I downloaded to the device and loop through the lines adding them to the array?

I want them in an array like this format below:

local myArray = {{“question”,“answer 1”,“answer2”,“answer3”,“answer4”}, next line, etc. }

The text file will be like this:

Question
Answer1

Answer2

Answer3

Answer4

Question
Answer1

Answer2

Answer3

Answer4

etc.

Thanks for your help!

Warren

Hi Warren,

Please see the following guide about reading and writing files, in particular the “Reading Lines” sub-section.

http://docs.coronalabs.com/guide/data/readWriteFiles/index.html

Using that, you can process each line and add its contents to a table. If necessary, you can also “sub-process” each line by doing string matching on it, to determine which is the correct answer as it loops through the text file.

Take care,

Brent

Thanks, that is exactly what I need. I can add another array column to hold which one is the answer.

I do need to learn more about arrays now. I need to find the same sort or page about creating arrays. What I need to know is how to first create an empty array and then add to an array. Any chance you have something for this?

I just saw the page in the docs for inserting into a table but that showed for inserting a single value. Can you show me in code how to insert, well, really just add to the end of an array a set like {“a”,“b”,“c”,“d”} into an array?

Thanks!

Warren

Hi Warren,

Creating an empty array (table) is very standard, and you’ve probably done it before:

[lua]

local myArray = {}

[/lua]

Then, to add to it, you can simply use the “# +1” method:

[lua]

myArray[#myArray+1] = “newValue”

[/lua]

This will just add a new value at the next spot in the array, by counting the current number of items and adding another one.

Effectively, what’s happening is:

1st iteration: table is empty (0 elements). 0+1=1. Lua adds the new element at position 1.

2nd iteration: table has 1 element. 1+1=2. Lua adds the new element at position 2.

etc., etc…

Of course, this is only for an indexed array. If you’re inserting “dictionary” items (key-value pairs) then the counting system won’t work like this. But I think you’re using the indexed method, so don’t worry about it right now.

Brent

Hi Warren,

Please see the following guide about reading and writing files, in particular the “Reading Lines” sub-section.

http://docs.coronalabs.com/guide/data/readWriteFiles/index.html

Using that, you can process each line and add its contents to a table. If necessary, you can also “sub-process” each line by doing string matching on it, to determine which is the correct answer as it loops through the text file.

Take care,

Brent

Thanks, that is exactly what I need. I can add another array column to hold which one is the answer.

I do need to learn more about arrays now. I need to find the same sort or page about creating arrays. What I need to know is how to first create an empty array and then add to an array. Any chance you have something for this?

I just saw the page in the docs for inserting into a table but that showed for inserting a single value. Can you show me in code how to insert, well, really just add to the end of an array a set like {“a”,“b”,“c”,“d”} into an array?

Thanks!

Warren

Hi Warren,

Creating an empty array (table) is very standard, and you’ve probably done it before:

[lua]

local myArray = {}

[/lua]

Then, to add to it, you can simply use the “# +1” method:

[lua]

myArray[#myArray+1] = “newValue”

[/lua]

This will just add a new value at the next spot in the array, by counting the current number of items and adding another one.

Effectively, what’s happening is:

1st iteration: table is empty (0 elements). 0+1=1. Lua adds the new element at position 1.

2nd iteration: table has 1 element. 1+1=2. Lua adds the new element at position 2.

etc., etc…

Of course, this is only for an indexed array. If you’re inserting “dictionary” items (key-value pairs) then the counting system won’t work like this. But I think you’re using the indexed method, so don’t worry about it right now.

Brent