1847 posts
Location
/home/dannysmc95
Posted 12 December 2014 - 12:04 PM
I have a small array with two arrays in like so:
{
{
"Test1",
"Test2",
"Test3",
"Test4",
},
{
"Test5",
"Test6",
"Test7",
"Test8",
}
}
This is downloaded and created by a PHP script. It is stored to a file (for example): output, this is then loaded in read mode to the variable table1. When I use:
print(type(table1))
it will return table.
When I try:
for _, v in ipairs(table1) do
print(v)
end
I get nothing?
It's like it didn't even run that code?
7083 posts
Location
Tasmania (AU)
Posted 12 December 2014 - 12:41 PM
I get the impression you're doing something like this:
table1 = fs.open("file","r")
… in which case table1 will point to a table containing functions for dealing with your file. Since these functions use keys for their names, ipairs won't pick up on them (though pairs will - assuming I'm right, if you switched to that you'd see a bunch of function pointers listed out).
You want to open the file, read the contents, unserialise them, then iterate through the results of
that:
local myFile = fs.open("file","r")
local table1 = textutils.unserialize(myFile.readAll())
myFile.close()
for _, v in ipairs(table1) do
print(v)
end
1847 posts
Location
/home/dannysmc95
Posted 12 December 2014 - 02:11 PM
I get the impression you're doing something like this:
table1 = fs.open("file","r")
… in which case table1 will point to a table containing functions for dealing with your file. Since these functions use keys for their names, ipairs won't pick up on them (though pairs will - assuming I'm right, if you switched to that you'd see a bunch of function pointers listed out).
You want to open the file, read the contents, unserialise them, then iterate through the results of
that:
local myFile = fs.open("file","r")
local table1 = textutils.unserialize(myFile.readAll())
myFile.close()
for _, v in ipairs(table1) do
print(v)
end
I shall try that, but I use a PHP generator that gives me the table like: {{"something1", "something2"},{"something3", "something4",},}
Then I save it to a table using:
file = fs.open("output", "w")
file.write(file) or file.write(file.readAll()) Think I am using readAll not sure, did it earlier.
file.close()
Then trying to load it
will try your idea now though!
1847 posts
Location
/home/dannysmc95
Posted 12 December 2014 - 02:19 PM
I get the impression you're doing something like this:
table1 = fs.open("file","r")
… in which case table1 will point to a table containing functions for dealing with your file. Since these functions use keys for their names, ipairs won't pick up on them (though pairs will - assuming I'm right, if you switched to that you'd see a bunch of function pointers listed out).
You want to open the file, read the contents, unserialise them, then iterate through the results of
that:
local myFile = fs.open("file","r")
local table1 = textutils.unserialize(myFile.readAll())
myFile.close()
for _, v in ipairs(table1) do
print(v)
end
Ahh got it working:
temp1 = fs.open("output", "r")
table1 = textutils.unserialize(temp1.readAll())
temp1.close()
for i = 1, #table1 do
for k, r in ipairs(table1[i]) do
i = i + 1
print(k..": "..r)
end
end
Thanks!!