How can I know return value count of a Lua function from C? - dynamic

luaL_loadstring(L, "return 3, 4, 5");
int R = lua_pcall(L, 0, 3, 0);
Lua can return multiple values. But currently I have to hardcode the count of the return values. Can I know the count at runtime dynamically?

Yes.
int top = lua_gettop(L);
luaL_loadstring(L, "return 3, 4, 5");
int R = lua_pcall(L, 0, LUA_MULTRET, 0);
int nresults = lua_gettop(L) - top;
You use LUA_MULTRET, and then use lua_gettop to figure out the top of the stack before and after the call.

Related

What is the Kotlin way of printing IntArray contents?

What is the Kotlin way of printing IntArray contents?
class Solution {
fun plusOne(digits: IntArray): IntArray {
println(digits.toString()) // does not work
println(Arrays.toString(digits)) // does work but its java way of doing
for(i in 0 until digits.size) {
...
}
return digits
}
}
Is there any kotlin method which works similar to Arrays.toString() ? I just want to see the content for debugging purpose.
There are multiple ways, depending on your requirement, you can use any. Note that you don't have to convert it to List just to print, unless you need it for other use cases.
println(arr.contentToString())
//this one prints number on each line
arr.forEach(::println) // same as arr.forEach{println(it)}
You can also use Arrays.toString but contentToString() is convenient, and it internally calls Arrays.toString
After doing a bit more research, I wanted to add joinToString() to above awesome answers :
val numbers = intArrayOf(1, 2, 3, 4, 5, 6)
println(numbers.joinToString())
This will print below output :
1, 2, 3, 4, 5, 6
This also allows you add prefix, suffix of your choice as below :
println(numbers.joinToString(prefix = "{", postfix = "}"))
This would output :
{1, 2, 3, 4, 5, 6}
You can use the contentToString function available for all types of Array
val digits = intArrayOf(1,2,3,4)
val javaToString = Arrays.toString(digits).also(::println) // [1, 2, 3, 4]
val kotlinToString = digits.contentToString().also(::println) // [1, 2, 3, 4]
println(javaToString == kotlinToString) // true
Here is a working example https://pl.kotl.in/dUu_aioq0
There are many ways to accomplish it.
One way would be to first build the output string using fold and then print it inside an also function:
val digits = intArrayOf(1,2,3,4,5,6,7)
digits.fold("[") { output, item -> "$output $item" }.also { println("$it ]") }
This would output:
[ 1 2 3 4 5 6 7 ]
Try to convert to list
fun main() {
val digits: IntArray = IntArray(10)
println(digits.toList()) // or digits.asList()
}
Output:
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

Cannot read property of undefined Processing.js

I have made an object with the object elevation, yet when a try accessing it using an array, the debugger states it is undefined.
var h = 30;
var disk = function(pos,elv){
this.pos = pos;
this.elv = elv;
rect(56 * pos, 369-pos * h + h, 95, h, 2);
};
var disks = [
{position:1,
elevation:1},
{position:1,
elevation:2},
{position:1,
elevation:3},
{position:1,
elevation:4},
{position:1,
elevation:5},
{position:1,
elevation:6}
];
fill(0, 136, 255);
for(var i = 0; i<= 6; i++){
var diskNum = disks[i];
disk(1,diskNum.elevation);
}
};
I expected the program to draw the rectangles on top of each other, but the program displays Cannot read property elevation of undefined.
https://www.khanacademy.org/computer-programming/new-program/5983920391094272
You have 6 elements in your disks array, at indexes 0, 1, 2, 3, 4, and 5.
Then you use this for loop to iterate over them:
for(var i = 0; i <= 6; i++){
Since you're using the <= operator, this includes 7 total indexes: 0, 1, 2, 3, 4, 5, and 6.
You probably just want the < operator instead.
Also note that it doesn't make a ton of sense to store this.pos and this.elv since you never use them for anything.
When I fix the above error, I see a rectangle. Not quite sure what the program is meant to do, but this at least fixes your error.

Assign 3 typed numbers to a variable in Processing

I am making a paint program in Processign and want to allow the user to type 3 numbers to change int r;.
I want to know if I could do something to take three typed numbers and assign them to a single variable like int r. Such as typing 2, 5, 5, and storing them as int r =255;
You could use an array.
An array is a variable that holds multiple values.
int[] r = {1, 2, 3};
int x = r[0];
Here is the Processing array reference.
You could create your own class.
Better yet, you could create a class that keeps track of your 3 values:
class MyNumbers{
int r;
int g;
int b;
public MyNumbers(int r, int g, int b){
this.r = r;
this.g = g;
this.b = b;
}
}
Then you just create an instance of that class and pass in your values:
MyNumbers rgb = new MyNumbers(1, 2, 3);
int r = rgb.r;
Here is the Processing class reference.
You could use the color type.
If all you want to store are rgb values, consider using the existing color type in Processing:
color c = color(1, 2, 3);
int r = red(c);
Here is the Processing color reference.
You also might want to look into the ArrayList or HashMap classes.

Error OCI_INVALID_HANDLE in Oracle OCI C Code

I get an error of invalid Handle when OCIStmtFetch2 function executes in my code.
char *query = "SELECT id FROM id_table WHERE ROWNUM <= :1";
rc = OCIStmtPrepare(stmt, errhp, (OraText*)query, strlen(query), OCI_NTV_SYNTAX, OCI_DEFAULT);
OCIBind *bindp = NULL;
sb2 pos = 1;
int key=13;
rc = OCIBindByPos(stmt, &bindp, errhp, 1, &key, sizeof(int), SQLT_INT, (dvoid*)&pos, NULL, NULL, 0, NULL, OCI_DEFAULT);
char output[key][120];
sb2 output_ind[1];
ub2 output_len[1];
ub2 output_code[1];
OCIDefine *defnpp;
rc = OCIDefineByPos(stmt, &defnpp, errhp, 1, (dvoid*)output, 120, SQLT_STR, (dvoid*)output_ind, output_len, output_code, OCI_DEFAULT);
int rows = key;
/* execute */
rc = OCIStmtExecute(svchp, stmt, errhp, key, 0, NULL, NULL, OCI_DEFAULT);
rc = OCIStmtFetch2(stmt, errhp, 0, OCI_DEFAULT, 0, OCI_DEFAULT);
If I bind the placehoder :1 with integer value 12 then the code works, any value greater than 12 gives me Error OCI_INVALID_HANDLE error after running OCIStmtFetch2. I have the following code to catch error, which I run after each OCI functions mentioned in the code above
if (rc != OCI_SUCCESS && rc != OCI_SUCCESS_WITH_INFO) {
report_error(checkerr("Function Name()", errhp, rc));
return 1;
}
Can anyone please help me what is incorrect in my code? Thank you!
It's not working for any value greater than 12 because you are defining your output array to be char output[13][120]. I think you already got the idea from Joachim's comment that your output buffer is insufficient for more rows.
I'll try to answer your follow-up question about ind, rlen and rcode variables in the function call -
rc = OCIDefineByPos(stmt, &defnpp, errhp, 1, (dvoid*)output, 120, SQLT_STR, (dvoid*)output_ind, output_len, output_code, OCI_DEFAULT);
OCIDefineByPos associates an item in a select list with the type and output data buffer.
output_ind here is a pointer to an indicator array. Each bind and define OCI call has a parameter that associates an indicator variable, or an array of indicator variables, with a DML statement, a PL/SQL statement, or a query. Here you can know more about indicator variables and their usage.
output_len here is a pointer to array of length of data fetched.
output_code here is pointer to array of column-level return codes.

cvMatchTemplate score

I am using openCV's cvMatchTemplate method to find a smaller image in a bigger image. The \ method below is matching the existing pattern object with the given 'current' image, the found pattern is marked by a rectangle drawn on the current image. When the pattern is visible its finding it reliably, when not its drawing somewhere "random" which makes sense.
I have trouble computing the score of the template matching. Is there an easy to determine that? I was looking on various sites but could not find a solution and also been looking at the values computed by cvMinMaxLoc but could not find a way to determine the quality of the match.
- (void)detect:(IplImage *)current
{
int patchx = pattern->width;
int patchy = pattern->height;
int iwidth = current->width - patchx + 1;
int iheight = current->height - patchy + 1;
IplImage *result=cvCreateImage( cvSize(iwidth,iheight),IPL_DEPTH_32F, 1);
cvMatchTemplate( current, pattern, result, CV_TM_SQDIFF );
CvPoint minloc, maxloc;
double minval, maxval;
cvMinMaxLoc( result, &minval, &maxval, &minloc, &maxloc, 0 );
/* draw red rectangle */
cvRectangle( current,
cvPoint( minloc.x, minloc.y ),
cvPoint( minloc.x + patchx, minloc.y + patchy ),
cvScalar( 0, 255, 0, 0 ), 1, 0, 0 );
}
Use a normed metric like CV_TM_CCORR_NORMED or CV_TM_SQDIFF_NORMED and compare the minimum value to a threshold (between 0 and 1.0).
cvMatchTemplate( current, pattern, result, CV_TM_SQDIFF_NORMED);
if (minval < THRESHOLD) {
/* draw red rectangle */
cvRectangle( current,
cvPoint( minloc.x, minloc.y ),
cvPoint( minloc.x + patchx, minloc.y + patchy ),
cvScalar( 0, 255, 0, 0 ), 1, 0, 0 );
} else {
//not found
}