I have a question about my self coded informix function.
Does anyone have any idea why the return value of the function is 'x'?
The SELECT statement of the for each head should be return 19 datasets.
Therefore, I would expect the 'x' is to be cascaded 19 times with itself
so the return value of the function is 'xxxxxxxxxxxxxxxxxxx'
The function which I wrote is:
CREATE function mydb.my_function(spAuftNr INTEGER, spPos INTEGER, spUPos INTEGER, spTeileNr INTEGER)
RETURNING VARCHAR;
DEFINE spWs_kz CHAR(1);
DEFINE spLfdnr CHAR(2);
DEFINE spLaenge CHAR(10);
DEFINE spBreite CHAR(10);
DEFINE spWegfall CHAR(1);
DEFINE spDicke CHAR(8);
DEFINE spReturn VARCHAR;
DEFINE tempy VARCHAR;
LET spReturn = '';
FOREACH SELECT to_char(ws_kz),to_char(lfdnr),to_char(laenge),to_char(breite),to_char(wegfall),to_char(dicke)
INTO spWs_kz, spLfdnr, spLaenge, spBreite, spWegfall, spDicke from my_table where auftnr = spAuftNr and pos = spPos and u_pos = spUPos and teile_nr = spTeileNr
LET spReturn = spReturn || 'x';
END FOREACH
RETURN (spReturn);
END function;
I hope someone can help me.
I think you know what is the next step when i can
realize this.
Best regards
Simon
I don't know what was wrong with me yesterday, jrenaut is right. The code should actually be like this::
CREATE function mydb.my_function(spAuftNr INTEGER, spPos INTEGER, spUPos INTEGER, spTeileNr INTEGER)
RETURNING VARCHAR(255);
DEFINE spWs_kz CHAR(1);
DEFINE spLfdnr CHAR(2);
DEFINE spLaenge CHAR(10);
DEFINE spBreite CHAR(10);
DEFINE spWegfall CHAR(1);
DEFINE spDicke CHAR(8);
DEFINE spReturn VARCHAR(255);
DEFINE tempy VARCHAR;
LET spReturn = '';
FOREACH SELECT to_char(ws_kz),to_char(lfdnr),to_char(laenge),to_char(breite),to_char(wegfall),to_char(dicke)
INTO spWs_kz, spLfdnr, spLaenge, spBreite, spWegfall, spDicke from my_table where auftnr = spAuftNr and pos = spPos and u_pos = spUPos and teile_nr = spTeileNr
LET spReturn = spReturn || 'x';
END FOREACH
RETURN (spReturn);
END function;
Thanks again for the effort.
Related
guys!
Can you help me, please!
I want to add a number to a table, and I want to have it like this:
A={1,2,3}
B=A+5
--- now B is {6,7,8}
I don't want to create any classes, additional modules. May be some kind of extension to global table?
I think it can be done via global __add overrides.. Any thoughts?
A = setmetatable({1,2,3},
{
__add = function (t, add)
assert(type(add) == "number", "invalid addend! number expected")
local result = {}
for i,v in ipairs(t) do
result[i] = v + add
end
return result
end
})
B = A + 5
print(table.concat(B, ","))
C = A + "f"
You can do this by iterating through the table with ipairs and setting each element of B to the sum of the number and the corresponding element of A
For example
local B = {};
local numberToAdd = 5;
for i, v in ipairs(A) do
B[i] = v + numberToAdd;
end
A metatable can also be used to have the syntax B = A + 5. You would use the same code as the example, but you would need to use setmetatable on A then set the __add function of the metatable to the example code.
I have stumbled upon this line of code and I am not sure what the [ ? ] part represents (my guess is it's a sort of a wildcard but I searched it for a while and couldn't find anything):
['?'] = function() return is_canadian and "eh" or "" end
I understand that RHS is a functional ternary operator. I am curious about the LHS and what it actually is.
Edit: reference (2nd example):
http://lua-users.org/wiki/SwitchStatement
Actually, it is quite simple.
local t = {
a = "aah",
b = "bee",
c = "see",
It maps each letter to a sound pronunciation. Here, a need to be pronounced aah and b need to be pronounced bee and so on. Some letters have a different pronunciation if in american english or canadian english. So not every letter can be mapped to a single sound.
z = function() return is_canadian and "zed" or "zee" end,
['?'] = function() return is_canadian and "eh" or "" end
In the mapping, the letter z and the letter ? have a different prononciation in american english or canadian english. When the program will try to get the prononciation of '?', it will calls a function to check whether the user want to use canadian english or another english and the function will returns either zed or zee.
Finally, the 2 following notations have the same meaning:
local t1 = {
a = "aah",
b = "bee",
["?"] = "bee"
}
local t2 = {
["a"] = "aah",
["b"] = "bee",
["?"] = "bee"
}
If you look closely at the code linked in the question, you'll see that this line is part of a table constructor (the part inside {}). It is not a full statement on its own. As mentioned in the comments, it would be a syntax error outside of a table constructor. ['?'] is simply a string key.
The other posts alreay explained what that code does, so let me explain why it needs to be written that way.
['?'] = function() return is_canadian and "eh" or "" end is embedded in {}
It is part of a table constructor and assigns a function value to the string key '?'
local tbl = {a = 1} is syntactic sugar for local tbl = {['a'] = 1} or
local tbl = {}
tbl['a'] = 1
String keys that allow that convenient syntax must follow Lua's lexical conventions and hence may only contain letters, digits and underscore. They must not start with a digit.
So local a = {? = 1} is not possible. It will cause a syntax error unexpected symbol near '?' Therefor you have to explicitly provide a string value in square brackets as in local a = {['?'] = 1}
they gave each table element its own line
local a = {
1,
2,
3
}
This greatly improves readability for long table elements or very long tables and allows you maintain a maximum line length.
You'll agree that
local tbl = {
z = function() return is_canadian and "zed" or "zee" end,
['?'] = function() return is_canadian and "eh" or "" end
}
looks a lot cleaner than
local tbl = {z = function() return is_canadian and "zed" or "zee" end,['?'] = function() return is_canadian and "eh" or "" end}
In the below code, can anyone explain why does t1:print() works but (t1):print fails. I am attempting to make something like (t1 * 3):print() work without using an intermediate variable.
function classTestTable(members)
members = members or {}
local mt = {
__metatable = members;
__index = members;
}
function mt.print(self)
print("something")
end
return mt
end
TestTable = {}
TestTable_mt = ClassTestTable(TestTable)
function TestTable:new()
return setmetatable({targ1 = 1}, TestTable_mt )
end
TestTable t1 = TestTable:new()
t1:print() -- works fine.
(t1):print() -- fails with error "attempt to call a boolean value"
Lua expressions can extend over multiple lines.
print
(3)
Will print 3
So
t1:print()
(t1):print()
actually is equivalent to
t1:print()(t1):print()
or
local a = t1:print()
local b = a(t1)
b:print()
So you're calling the return value of t1:print()
To avoid that follow Egors advice and separate both statements with a semicolon.
t1:print();(t1):print()
I know I can use debug.getinfo(1, "n").name to get the calling function's name, but I would like to get a reference to that function pointer itself.
For debug.getlocal(), the f parameter is the stack position so I can easily get the locals for the calling function by just choosing the correct index. But for debug.getupvalue(), the f parameter is the function pointer itself, which I don't have.
Here is a brief example, but with the offending line debug.getupvalue(someFunction, index) there to demonstrate what I would like to accomplish, without the hard-coded reference.
local someUpValue = "stackoverflow"
function someFunction()
local value1 = "hi"
local value2 = "there"
local value3 = someUpValue
log()
end
function log()
local callingFuncName = debug.getinfo(2, "n").name
local index = 1
while(true) do
local name, value = debug.getlocal(2, index)
if name then
print(string.format("Local of %s: Name: %s Value: %s", callingFuncName, name, value))
else
break
end
index = index + 1
end
index = 1
while(true) do
local name, value = debug.getupvalue(someFunction, index)
if name then
print(string.format("Upvalue of %s: Name: %s Value: %s", callingFuncName, name, value))
else
break
end
index = index + 1
end
end
someFunction()
You can use debug.getinfo(2, "f").func to get the function reference (assuming you are calling from the function you want to get a reference to):
function log()
local callingFuncRef = debug.getinfo(2, "f").func
callingFuncRef(false) -- this will call the function, so make sure there is no loop
How can I make a changing variable an element of a table, like so.
local table = {}
local var = 10
Now I want to insert this variable as an element of table.
Something like this:
table[1] = var
What I need is, whenever I call this table[1], even if the variable changes, it will call the actual value of that variable, like this:
print(table[1]) -> prints 10
var = var + 5
print(table[1]) -> prints 15
Is this even possible somehow?
EDIT:
What I want to accomplish is the following: I want to have an element in table that says which variable should be shown. For example:
local var1 = 10
local var2 = 20
Now if I have a table that has elements as strings of those variables like so:
local table = {"var1", "var2"}
Now if I do print(table[1]), of course it will print out "var1", but is there any way I could turn this element of a table that is string into a call for variable when I actually need that variable. You might be asking why don't I just call the var1, but there is a reason which I can explain, but it would be really long. Let's say I just need it this way. Also, the var1/var2 CAN CHANGE.
You have a couple of choices.
1) Field function as a closure over var: Straightforward but requires changes how you use it
local t = {}
local var = 10
t.varf = function() return var end -- varf could be named var but that might be confusing
var = var + 5
print(t.varf()) -- call it to get the value
2) __index metamethod to avoid the explicit function call syntax
local t = {}
local var = 10
setmetatable(t, {
__index = function(_, k)
if k=="var" then return var else return nil
end})
var = var + 5
print(t.var) -- t does not contain a field with key "var" so __index is called
(The __index function is also a closure over var.)
If you want to modify var via t, then look to the __newindex metamethod.
Both methods use closures. A closure is a function that refers to non-global variables outside of its parameters and body.
Numbers in Lua are shared by copy, at time of assignment. The table[1] and var both receive their own, independent copy of the number 10.
If you want to share numbers, you'll need to encapsulate them in their own table.
local table = {}
local var = { value = 10 }
table[1] = var
print(table[1].value) -- prints 10
var.value = var.value + 5
print(table[1].value) -- prints 15
You can also consider creating some kind of abstraction over numbers. A quick example. You'll need to make sure your operations are well defined, though.
local number = {}
number.__index = number
local function Number (value)
return setmetatable({ value = value }, number)
end
function number.__add (a, b)
if type(b) == 'number' then
return Number(a.value + b)
elseif getmetatable(b) == number then
return Number(a.value + b.value)
end
error("one of `number, Number' expected")
end
function number:add (n)
if type(n) == 'number' then
self.value = self.value + n
elseif getmetatable(n) == number then
self.value = self.value + n.value
else
error("one of `number, Number' expected")
end
return self.value
end
function number.__tostring (v)
return v.value .. ''
end
local foo = {}
local bar = Number(10)
foo[1] = bar
print(foo[1]) -- 10
bar:add(5)
print(foo[1]) -- 15
print(bar + 25) -- 40