LUA : Use table from another file - lua

I just studied Lua language. I am very confused about it.
I have two file
string.lua
STRINGS=
{
CHARACTER_NAMES =
{
web = "webby",
sac = "sacso",
}
}
STRINGS.BUNNYNAMES =
{
"Brassica",
"Bunium",
"Burdock",
"Carrot",
}
And I have generate.lua file for get value form table in string.lua.
Then print value.
But I don't know how to acess table form another file.
I want to use STRINGS and STRINGS.BUNNYNAMES too,
Please advise me.

string.lua defines one global variable named STRINGS which contains a table.
You need to execute string.lua before you can access STRINGS. Just do dofile("string.lua") for instance.

Related

Simpler Way to Add a new Field to Object in Rails

I have a field in animal_food (a json object) called treats and I want to add a field in treats called time_eaten and then return everything from the field treats.
def addTime(time)
animal_food["treats"]["time_eaten"] = time
animal_food["treats"]
end
Is there a way I can do the code above in 1 line (using merge or other ruby syntax?)
Yes:
animal_food['treats'].merge!({'time_eaten' => time})
does the same thing as those two lines. (Note the exclamation mark.) You could also say
animal_food['treats'].tap { |treats| treats['time_eaten'] = time }
Or, in Ruby 2.7 preview, using numbered parameters:
animal_food['treats'].tap { #1['time_eaten'] = time }

Objective-C iOS parse string according to the definition

I want create an iOS app for my school.
This App will show Week schledule and when I tap on Cell with Subject, it will show me detail info about subject...
My problem:
Our teachers use shotcuts for their names, but I want show their full name... I created the file "ucitele.h" with definitions of their names, but I don't know, how to use it 😕.
This is how that file looks:
//
// ucitele.h
//
#define Li #"RNDr. Dan---vá"
#define He #"Mgr. Ja---hl"
#define Sm #"Ing. Mich---rek"
#define Ks #"Mgr. Svat---á"
I get the shortcut of Teacher from previous view from "self.ucitel" and I maybe want compare the contents of the "self.ucitel" with definitions and set the "ucitelFull" string from the definitions? I don't know how to say it 😕.
when the content of the self.ucitel will be #"Sm", than I want parse "ucitelFull" as #"Ing. Mich---rek"
Answers in Objective-C only please
Okay, sounds like your trying to map a short identifier to a full name:
-(NSString*)fullNameFromShortName:(NSString*)short {
NSDictionary * names = #{#"Li" : #"RNDr. Dan---vá",
#"He" : #"Mgr. Ja---hl", ... };
return [names objectForKey:short];
}
Use like:
self.ucitelFull = [self fullNameFromShortName:self.ucitel];
This is a dictionary that has the short name as a key and the full name as the value.
Some further suggestions:
try using lowercase keys and comparing lowercaseString's, incase the user doesn't enter the value with the correct case.
You can move the dictionary definition into a json file and read it from your bundle, to eliminate the hardcoding

How to add custom facters in Ruby

I have file text structured as follows
key1,value1
I want to add these key values as facters in my host. I have splitted those key (configs[0]) , values (configs[1]). How to add those to facters using ruby. I have tried following code but not succeeded
configs = File.read("........configuration.txt").split(",").map(&:strip)
Facter.add(configs[0]){
setcode { configs[1])
}
Regards,
Malintha

How to load table from file

I know how to write table content in a text file and restore it. But what is the best practice to write a custom table type in a file?
Here is my situation:
I have a list of tables f.e. objective1 = {} objective2 = {} objective3 = {} ..
Each objective has its own execute function which checks some conditions and execute some commands depending on the conditions.
Each new game a pick a random set of objectives and store them in an array Objectives = { [1] = { objective1 , objective3 } }
Now I want to save the array of random objectives and load them later from the file.
Is there any possibility to save the name of the tables in the array and to restore them by the name?
Would be great to have a solution without using a factory pattern or using indices for saving.
If any of the data in the table is not a userdata (for eg. a SQL or socket connection, or other libraries) you can use the pickle module to write the data to a file and later load the same from there.
I personally use a different pickle library (written by Walter Doekes). The usage is as follows:
local TableToStore = { 'a', 'b', 1, {'nested', 'content', {c = 'inside'}} }
pickle.store( 'backupFileName.bak', {TableToStore = TableToStore} )
and to restore the data, I can simply use
dofile "backupFileName.bak"
and I'll get back a table named TableToStore after this.

jQuery autocomplete not displaying my encoded values

I am working from this example: http://jqueryui.com/demos/autocomplete/#remote and I am encoding the output like this:
$rows = array();
while($r = mysql_fetch_assoc($category_result))
{
$rows[] = $r;
error_log ("rows: ".$rows[0]);
}
echo json_encode($rows);
But the dropdown on the other side shows nothing. Here is my test page: http://problemio.com/test.php - if you enter "ho" it matches 2 results in the database, but they are not getting displayed for some reason. Any idea why?
Thanks!!
The properties should be named label and value. From the JQuery UI demo page you linked to:
The local data can be a simple Array of Strings, or it contains
Objects for each item in the array, with either a label or value
property or both. The label property is displayed in the suggestion
menu.
So you would need to rename category_name to label either in PHP or later on in your JavaScript source handler function. The latter would require you to replace the PHP URL with a callback function like in the remote example. That way you could get the data any way you want (e.g. by jQuery.getJSON()) and work with it before it gets handed over to the suggestion box.
Hope this helps.
Regarding your comment, this should do it:
$rows = array();
while ($r = mysql_fetch_array($category_result)) {
$rows[] = array("label" => $r["category_name"]);
}
echo json_encode($rows);

Resources