Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (Flatly)
  • No Skin
Collapse
Brand Logo

NetXMS Forum

  1. Home
  2. Support
  3. Returning array from custom function in NXSL

Returning array from custom function in NXSL

Scheduled Pinned Locked Moved Support
5 Posts 3 Posters 19.9k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M
    M
    mmladinov
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • T
      T
      tomaskir
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • M
        M
        mmladinov
        wrote on last edited by
        #3

        @tomaskir said in Returning array from custom function in NXSL:

        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.

        1 Reply Last reply
        0
        • V
          V
          Victor Kirhenshtein
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          • M
            M
            mmladinov
            wrote on last edited by
            #5

            @victor said in Returning array from custom function in NXSL:

            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.

            1 Reply Last reply
            0

            Hello! It looks like you're interested in this conversation, but you don't have an account yet.

            Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

            With your input, this post could be even better 💗

            Register Login
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            Powered by NodeBB Contributors
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups