read a file and use the data to make a variable - lua

So I'm trying to read a file, written with the format of a table:
({loop=true, test=1})
So basicly I want to read this data with this function:
function readAll(file)
local f = assert(io.open(file, "rb"))
local content = f:read("*all")
f:close()
return content
end
after I got the content of the file I use table.insert to store my data in an existing table..
but it seems like it stores my data as a string in the table, so if I try to get the data like this:
print(tablename.loop)
it returns nil, cause it hasn't made the table as I wanted it, it just pasted it as a string.
if I unpack the table I get the exact same thing as the code field at the top. A string.
So I try to insert it in the table, as valid variables, that I can read then easily with tablename.loop etc.
Sorry for my probably bad english.. I hope someone can help me.

thanks #Egor Skriptunoff. this is the solution I was looking for <3
since he only made a comment, I'll write his solution down again:
tablename = assert((loadstring or load)("return "..readAll(file)))(); print(tablename.loop)
everyone have a nice day!

Related

How do i read strings on separate lines

I'm trying to read strings on separate lines/space as title says.
I have a row on phpmyadmin which is:
I have a made a function to get the list value on Lua and the next step would be to read every name separately and thats what i can't figure it out.
Its possible to read every line separately and then add them to an array?
As for example
local names = {Noba, Detalle}
Solved:
local names = {}
for _, name in ipairs(getList():explode("\n"))
table.insert(names, name)
end

Reserving rows from Parse using Pointers

I'm trying to retrieve rows from a class called Consultations using a column called userPointer. It should be straight forward but I've been struggling with this for couple of days:
Let us for example say I would like to get the first object
where userPointer = ttnRYrdu0J
Note: I will replace ttnRYrdu0J with PFUser.current() later to be dynamic if this works.
I wrote the code like this:
Unfortunately it doesn't work, however when I use any other columns such as objectId or type it works fine.
Any help please!!
Thanks
I've got the answer I used PFUser.current() instead of hard coding it which is what I want at the end because I want to show the consultations of the current user and it worked! I'm still confused why it is not working when I hard code the value but I got what I want.

Writing List of object into CSV File in xamarin.Android

Can any one suggest me How to implement writing List of objects into .csv into device download storage in c# xamarin android.The data exported to csv should be having headers.each item in list should be written in row .something similar to image attached
To create a CSV string, you'd generally do this manually. As an example:
string headers = $"{Column1},{Column2}";
string row1 = $"{row1.ValueA},{row1.ValueB}";
...etc...
Then append them together, separated by newlines. Obviously you'd be better off doing the above in some kind of loop.
As for putting it into android download storage, someone else will have to tell you how to do that, I don't know how!
i have followed below link http://www.joe-stevens.com/2009/08/03/generate-a-csv-from-a-generic-list-of-objects-using-reflection-and-extension-methods/ that helped me to implement.

Swift ios, sqlite, error 21 Library used incorrectly

I am a beginner in swift language (for IOS) and I am going to use sqlite in my program.
I added the libsqlit3.dylib to my project and used bridging-header. (the wrapper is downloadded from : https://github.com/chrismsimpson/SwiftSQLite)
I have run the following code :
var db = SQLiteDatabase();
db.open("test.db");
var statement = SQLiteStatement(database: db);
var sql = "CREATE TABLE IF NOT EXISTS CONTACTS (ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT, ADDRESS TEXT, PHONE TEXT)"
println("result : \(statement.prepare(sql))");
the result code is 21 which means "Library used incorrectly".
the test.db file doesn't exist and expect it will be created.
I dont know what is the problem! please help me.
It seems the problem is because of the "test.db" dosent exists.
Now the question is how can I create the "test.db" file. Why the swift dosnt create the database file when the file doesnt exist?
I intentionally left this part simple, because it's likely people want to have their own logic with regards to CRUD operations around the database file itself.
But, for the sake of completeness I've added some more boilerplate code to take care of this all for you. Please read the README on the github page, but suffice it to say, if you do everything as described, now calling SQLiteDatabase.sharedInstance will automagically create a new database if one doesn't exist already. You'd use this as follows:
var statement = SQLiteStatement(database: SQLiteDatabase.sharedInstance)

ruby - How can I associate text with a construction point with sketchup api?

I'm developing a sketchup plugin with ruby, I have coded the parsing process succesfully and I got the cpoints from csv file to sketchup. The csv file also contains a description within coordinates of every point like : ["15461.545", "845152.56", "5464.59", "tower1"].
I want to get the tower1 as a text associated to every point.
How can I do that ?
PS : You don't need to get the tower1 from the array, i've already done that. I have it now in an independant variable like :
desc_array = ["tower1", "beacon48", "anna55", ...]
Please help me
I did that by
##todraw_su.each do |todraw|
##desc_array.each do |desc|
#model.entities.add_text desc, todraw
end
end
But I found a problem with each statement, it loops for every todraw in desc
If you know how I can do it, answer on this question Each statement inside another each malfunctions?

Resources