I would like to concatenate a variable with a table in a file called items.json. My attributes inside of the files are called "pos1_name", "pos2_name", etc... So I would like to use the number in the attribute name, as a variable. I've try : item.pos..i.._name but it doesn't work.
items = VarLoad.loadTable("items.json")
for i=1,7 do
--var ="1"
--print(table.concat(items,"",2))
if items.pos1=="1" then
item[i] = display.newImageRect("items/"..items.pos1_name..".png", 80 , 80)
item[i].x = holder[i].x+10
item[i].anchorX=0
item[i].y=holder[i].y
itemGroup:insert (item[i])
item[i].destination="item1"
item[i]:addEventListener( "touch", onSwitchPress )
item_bigsize[i] = display.newImageRect("items/"..items.pos1_name..".png", 240 , 240)
item_bigsize[i].x = 950
item_bigsize[i].anchorX=0
item_bigsize[i].y=display.contentCenterY-130
group:insert (item_bigsize[i])
item_bigsize[i].isVisible=true
end
i=i+1
end
My attributes inside of the files are called "pos1_name", "pos2_name", etc... So I would like to use the number in the attribute name, as a variable. I've try : item.pos..i.._name but it doesn't work.
You need to use full table notation: items.pos1_name is a shortcut for items['pos1_name'], so if you need to generate the key dynamically, you can use something like this:
print(items['pos'..i..'name'])
Related
First of all, thanks for helping me.
My question, lets say I have readme.txt file, and inside looks like below
a1 3
b2 4
c3 -2.3
d23 55.6
Now, how can I make a function to load this txt file, so that in the Octave, I will directly have
a1=3
b2=4
c3=-2.3
d23=55.6
Allow me say this again, DIRECTLY. Once I use this function "readFunction("readme.txt")", all those variables will be load, and ready to use.
I tried [name, num] = textread ("readme.txt", "%s %f"), "num" is the numbers, but I don't know how to convert the cell "name" to variable name. e.g it's wrong if I do char(name(1)) = b(1). (trying to do a1 = 3).
Or maybe my way is just completely wrong? Thanks for the help.
First, the idea:
Read line by line the file
Replace the space with and = symbol
Optionally add a ; symbol at the end of each line
eval the string.
Second, a simple code example:
filename="file.txt";
fid=fopen(filename);
line=fgetl(fid);
while line != -1
eval(strrep(line, ' ', '='));
line=fgetl(fid);
endwhile
fclose(fid);
I have an SSRS textbox currently using the following expression:
=Switch
(
First(Fields!MyAwesomeId.Value, "MyAwesomeDataset") = 0, First(Fields!MyAwesomeNonmessage.Value, "MyAwesomeDataset"),
First(Fields!MyAwesomeId.Value, "MyAwesomeDataset") = 1, First(Fields!MyAwesomeMessage.Value, "MyAwesomeDataset")
)
with it's markup type set to HTML.
The MyAwesomeMessage and MyAwesomeNonmessage fields both contain html break tags which according microsoft, don't render as such.
https://msdn.microsoft.com/en-us/library/cc645967.aspx
How might I replace all the break tags in the data fields MyAwesomeMessage and MyAwesomeNonmessage with the correct characters to cause a line break on the finished report?
You can wrap your switch expression in a replace that will swap out all the text you specify.
In your case this would probably look like this:
=Replace(Switch(
First(Fields!MyAwesomeId.Value, "MyAwesomeDataset") = 0
,First(Fields!MyAwesomeNonmessage.Value, "MyAwesomeDataset")
,First(Fields!MyAwesomeId.Value, "MyAwesomeDataset") = 1
,First(Fields!MyAwesomeMessage.Value, "MyAwesomeDataset")
)
,"<br>"
,vbcrlf
)
I want to extract the values within li elements and store them into variables.
Example:
<li>Male</li><li>Hustisford, WI</li><li>United States</li>
However, it could also be like this:
<li>Hustisford, WI</li><li>United States</li>
or no
I started with this:
author_origin = string.gsub(string.gsub(htmlcode,"<li>","#"),"</li>","|")
author_gender, author_orig_city, author_orig_country = string.match(author_origin,"#(.-)|#(.-)|#(.-)|")
=> this worked for the first example but not for the other cases.
I thought it should be something like this but it didn't work:
author_gender, author_orig_city, author_orig_country = string.match(author_origin,"[#]?(.-?)[|]?[#]?(.-?)[|]?[#]?(.-?)[|]?")
You can avoid needing multiple patterns by simply grabbing everything that matches your criteria and then figuring out what you have at the end. Something like this.
function extract(s)
local t = {}
for v in s:gmatch("<li>(.-)</li>") do
t[#t + 1] = v
end
if #t == 3 then
return (unpack or table.unpack)(t)
end
return nil,(unpack or table.unpack)(t)
end
author_gender, author_orig_city, author_orig_country = extract("<li>Male</li><li>Hustisford, WI</li><li>United States</li>")
print(author_gender, author_orig_city, author_orig_country)
author_gender, author_orig_city, author_orig_country = extract('<li>Hustisford, WI</li><li>United States</li>')
print(author_gender, author_orig_city, author_orig_country)
You can't do it with a single patten. You need two. First try for three fields. If it fails, try for two fields. And you don't need to replace the HTML tags with others characters.
author_gender, author_orig_city, author_orig_country = string.match(author_origin,"<li>(.-)</li><li>(.-)</li><li>(.-)</li>")
if author_gender==nil then
author_orig_city, author_orig_country = string.match(author_origin,"<li>(.-)</li><li>(.-)</li>")
end
If you need to parse unpredictable HTML and don't mind depending on a library, you could use lua-gumbo:
local gumbo = require "gumbo"
local input = "<li>Male</li><li>Hustisford, WI</li><li>United States</li>"
local document = gumbo.parse(input)
local elements = document:getElementsByTagName("li")
local gender = elements[1].textContent
local city = elements[2].textContent
local country = elements[3].textContent
print(gender, city, country)
I have a column which contains data like
thumb/RANDOM_STRING.JPG
thumb/cat/RANDOM_STRING.JPG
thumb/test/again/RANDOM_STRING.JPG
I want to contain only the Image name. And want to DELETE every word before / character.
Here is what I have tried but it does not work
UPDATE wsr_jshopping_products
SET product_full_image =
REPLACE(product_full_image,'thumb//', '');
if you just want the filename to remain use:
UPDATE wsr_jshopping_products
SET product_full_image = REVERSE(
SUBSTRING(
REVERSE(product_full_image),1,
LOCATE('/',REVERSE(product_full_image))-1))
I'm wondering if there is a way to specify if the parameters of a lua function should be copied or just referenced. Color is an object representing a color.
For example, with this code
function editColor(col)
col.r = 0
print(tostring(col.r))
end
color = Color(255, 0, 0)
print(tostring(color.r))
editColor(color)
print(tostring(color.r))
Makes the output
255
0
0
So col is a "reference" to color, but this code:
function editColor(col)
col = Color(0, 0, 0)
print(tostring(col.r))
end
color = Color(255, 0, 0)
print(tostring(color.r))
editColor(color)
print(tostring(color.r))
Makes this output
255
0
255
So here the color is copied.
Is there a way to force a parameter to be copied or referenced? Just like the & operator in C++?
No, parameters in Lua are always passed by value (mirror). All variables are references however. In your second example in editColor you're overriding what the variable col refers to, but it's only for that scope. You'll need to change things around, maybe instead of passing in a variable to be reassigned, have the function return something and do your reassignment outside. Good luck.
This will do what you want. Put the variable that you want to pass by reference into a table. You can use a table to pass anything by ref, not just a string.
-- function that you want to pass the string
-- to byref.
local function next_level( w )
w.xml = w.xml .. '<next\>'
end
-- Some top level function that you want to use to accumulate text
function top_level()
local w = {xml = '<top>'} -- This creates a table with one entry called "xml".
-- You can call the entry whatever you'd like, just be
-- consistant in the other functions.
next_level(w)
w.xml = w.xml .. '</top>'
return w.xml
end
--output: <top><next\></top>
Lua is a bad language. Suffer its simplicity when you need to do something just a little complex.