Populate an array with a text file?

I am trying to dynamically populate an array with the contents of a text file. I can’t
find any syntax examples.

This is what I have so far,however, it is not working:

local path = system.pathForFile( “test.data”, system.DocumentsDirectory )
local file = io.open( path, “r” )
local character_array = {}

for line in file:lines() do
for i = 1, #line do
character_array[#character_array + 1] = line[i];
end
end

io.close( file )
Any help would be greatly appreciated…Walt
Thanks! [import]uid: 9936 topic_id: 30814 reply_id: 330814[/import]

Do you want each array cell to hold one line of text from the file?

You’re code is close:

local path = system.pathForFile( "test.data", system.DocumentsDirectory )  
local file = io.open( path, "r" )  
local character\_array = {}  
  
for line in file:lines() do  
 character\_array[#character\_array + 1] = line;  
end  
  
io.close( file )  

Though I would have done it a big differently, yours may be the better way:

[code]
local path = system.pathForFile( “test.data”, system.DocumentsDirectory )
local file = io.open( path, “r” )
local character_array = {}

line = file:read("*l")
while line do
character_array[#character_array + 1] = line
line = file:read("*l")
end

io.close( file )
[/code] [import]uid: 19626 topic_id: 30814 reply_id: 123303[/import]

@ robmiracle

Thanks for that code snippet, however I think that I’m on the wrong path. I might need a dictionary.

This is what my test data looks like:

{ iconL = 17, caption = “Item 2” },
{ iconL = 17, caption = “Item 3” },
{ iconL = 17, caption = “Item 4” },
{ iconL = 18, caption = “Item 5” },
{ iconL = 18, caption = “Item 6” },
[import]uid: 9936 topic_id: 30814 reply_id: 123332[/import]

Grrr… I hate touch pads trying to navigate backwards when my swipe isn’t straight up and down…

Your data almost looks like JSON data. I think you just need square brackets [] around the outside. If your data is JSON formatted, then you can do something like this:

function loadTable(filename)  
 local path = system.pathForFile( filename, system.DocumentsDirectory)  
 local contents = ""  
 local myTable = {}  
 local file = io.open( path, "r" )  
 if file then  
 local contents = file:read( "\*a" )  
 myTable = json.decode(contents);  
 io.close( file )  
 print("Loaded file")  
 return myTable  
 end  
 print("file not found")  
 return nil  
end  
  
character\_array = loadTable("test.data")  

Then you would end up with:

character_array[1].icon
character_array[1].caption
character_array[2].icon
character_array[2].caption
character_array[3].icon
character_array[3].caption
character_array[4].icon
character_array[4].caption

type data structure to use.

The data in JSON would look like this:

[{ "iconL": 17, "caption": "Item 2" }, { "iconL": 17, "caption": "Item 3" }, { "iconL": 17, "caption": "Item 4" }, { "iconL": 18, "caption": "Item 5" }, { "iconL": 18, "caption": "Item 6" }] [import]uid: 19626 topic_id: 30814 reply_id: 123345[/import]

Do you want each array cell to hold one line of text from the file?

You’re code is close:

local path = system.pathForFile( "test.data", system.DocumentsDirectory )  
local file = io.open( path, "r" )  
local character\_array = {}  
  
for line in file:lines() do  
 character\_array[#character\_array + 1] = line;  
end  
  
io.close( file )  

Though I would have done it a big differently, yours may be the better way:

[code]
local path = system.pathForFile( “test.data”, system.DocumentsDirectory )
local file = io.open( path, “r” )
local character_array = {}

line = file:read("*l")
while line do
character_array[#character_array + 1] = line
line = file:read("*l")
end

io.close( file )
[/code] [import]uid: 19626 topic_id: 30814 reply_id: 123303[/import]

@ robmiracle

Thanks for that code snippet, however I think that I’m on the wrong path. I might need a dictionary.

This is what my test data looks like:

{ iconL = 17, caption = “Item 2” },
{ iconL = 17, caption = “Item 3” },
{ iconL = 17, caption = “Item 4” },
{ iconL = 18, caption = “Item 5” },
{ iconL = 18, caption = “Item 6” },
[import]uid: 9936 topic_id: 30814 reply_id: 123332[/import]

Grrr… I hate touch pads trying to navigate backwards when my swipe isn’t straight up and down…

Your data almost looks like JSON data. I think you just need square brackets [] around the outside. If your data is JSON formatted, then you can do something like this:

function loadTable(filename)  
 local path = system.pathForFile( filename, system.DocumentsDirectory)  
 local contents = ""  
 local myTable = {}  
 local file = io.open( path, "r" )  
 if file then  
 local contents = file:read( "\*a" )  
 myTable = json.decode(contents);  
 io.close( file )  
 print("Loaded file")  
 return myTable  
 end  
 print("file not found")  
 return nil  
end  
  
character\_array = loadTable("test.data")  

Then you would end up with:

character_array[1].icon
character_array[1].caption
character_array[2].icon
character_array[2].caption
character_array[3].icon
character_array[3].caption
character_array[4].icon
character_array[4].caption

type data structure to use.

The data in JSON would look like this:

[{ "iconL": 17, "caption": "Item 2" }, { "iconL": 17, "caption": "Item 3" }, { "iconL": 17, "caption": "Item 4" }, { "iconL": 18, "caption": "Item 5" }, { "iconL": 18, "caption": "Item 6" }] [import]uid: 19626 topic_id: 30814 reply_id: 123345[/import]