Returning array from custom function in NXSL

Started by mmladinov, March 21, 2016, 06:11:57 PM

Previous topic - Next topic

mmladinov

Hi. I'm new to NetXMS and I'm loving it - it's great.
I'm using ver 2.0.2, and I'm trying to write function for a common task in script library which should return array. However returned value is string with text that looks like it is reference to the array or similar. So I have in one script library called cfunctions:

sub getToken(data) {
text=data[1];
delimiter=data[2];
position=data[3];
i=index(text,delimiter,position);
if (i==0) {
token=substr(text,position);
position=null;
} else {
token=substr(text,position,i-position);
position=i+1;
}
return %(text,delimiter,position,token);
}


then in another script i have:
use cfunctions;
t="aa,bb,cc,dd";
delimiter=",";
position=1;
array data;
data[1]=text;
data[2]=delimiter;
data[3]=position;
data[4]="";1

data=cfunctions.getToken(data);
...


If I try to use data[1] after call to my function I get error that object is not a container, if I print typeof(data) I get that it is string, and if I print variable data  I get someting like: [A@0x7ff74c30c9f9].

I also tried to create array in getToken function and populate it one by one (array retr; retr[1]=..., retr[2], .... return retr;) but I get same problem.

So how can I return and use returned array from my function.

Thank you.

tomaskir

Can you replicate the problem in a simple to use function?

Using this in "Execute server script" works fine for example:

sub test() {
  return %(1,2,3);
}

return test()[1];



Moving the function "test()" to a library script named "test_func" and then running this script also works fine:

use test_func;

return test()[1];


So it sounds like your problem is somewhere else.

mmladinov

Quote from: tomaskir on March 21, 2016, 06:27:26 PM
Can you replicate the problem in a simple to use function?

[snip]

So it sounds like your problem is somewhere else.

Thank you for your reply. You are right. I tried your suggestion of simple function and it worked, so I tracked the problem to calling my function by library_name.function_name(), instead of just function_name().

I don't know if this other syntax of calling ( lib.fn() ) should work or not. I mean, in my code, the correct function was called even with lib.fn() syntax, and it did it's job, just the returning of an array was a problem. (I tested returning just a string and that worked fine).

Do you think this was just mistake in my code and it half-worked by accident, or is this a bug and I should report it somewhere? Anyway, thank you for your help.

Victor Kirhenshtein

Hi,

dot (.) is a string concatenation operation, so line

data=cfunctions.getToken(data);

concatenates value of variable cfunctions (likely null) with returned array, which resulted in a null converted to string concatenated with array converted to string.

Best regards,
Victor

mmladinov

Quote from: Victor Kirhenshtein on March 21, 2016, 07:48:12 PM
Hi,

dot (.) is a string concatenation operation, so line

data=cfunctions.getToken(data);

concatenates value of variable cfunctions (likely null) with returned array, which resulted in a null converted to string concatenated with array converted to string.

Best regards,
Victor

It all makes sense now. So easy when master explains it. :)

Thank you.