Announcement

Collapse
No announcement yet.

Returning Arrays from Internal Functions

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Returning Arrays from Internal Functions

    Hi,

    Do recent revisions support returning multiple values from internal function? It used not to.

    Example:

    function main() {
    ....
    var X = MyFunc1(parameters);
    var xx = X[0];
    var yy = X[1];
    var zz = X[2];
    ...
    }

    function MyFunc1(parameters) {
    ......
    return new Array(V0,V1,V2) //Vx are calculated by this function
    }

    If anybody can help it would be appreciated
    Thank you.

    Mihai
    Mihai Buta

  • #2
    Mihai
    I believe we have always had the option to retrieve the values of an array returned by a separate function.
    Anyhow you should be able to easily verify this with a simple efs.
    Alex

    Comment


    • #3
      So there is no way to know for sure if it is a feature that it is now supported?
      Trying is ok, but if does not work you always doubt. Besides, I dont think we should be guessing. We dont get eSignal for free.

      In the past we could plot multiple values, but could not retrieve them for further use in the efs.

      Does anybody know?

      Thank you.
      Mihai Buta

      Comment


      • #4
        Mihai
        To know if it works all you need to do is use your own example and replace V0, V1 and V2 with some values (for example 2, 4 and 6).
        Once you do that you will see that X[0], X[1] and X[2] will return those same values in main.
        As I said before I believe this functionality has always been available
        Alex

        Comment


        • #5
          Mihai,

          Here are some links to some efs's that I have used to return arrays from internal functions.




          Arrays EFS

          While I do not use the older methods much for returning arrays, they still work quite well.

          Also

          var a=new Array (3,4,5,6,7,8,9);
          return a;


          and

          return new Array (3,4,5,6,7,8,9);


          are functionally identical.

          Comment


          • #6
            Steve, Alex, thank you.

            I looked the old thread [where I was told specifically that this feature is not supported] but could not find it.

            That is why I asked if it is supported [officially] now.

            As I said, I know "it may work", but if it is not a supported feature [and nowhere is specified it is], you never know if it works in all conditions, or what conditions, so I will avoid all together.

            Mihai
            Mihai Buta

            Comment


            • #7
              Mihai,

              I was putting this together for you when you replied to the last post. This functionality has always been supported as it is a part of the basic JavaScript command set. My examples I provided dated back to late 2003. The example link below is from early 2004. If you were told that this functionality was not supported, it was either in error or a misinterpretation of your question or your misinterpretation of the response. As to whether you choose to ignore the functionality, that is your choice.

              Regardless, here was the post I was putting together, since I have put some time into this, and hate to waste the effort, I will post it anyways. Hopefully it will help someone.

              Here is another thread where you will find these techniques implemented in several posted efs's. check out the "weighted ma of regression.efs" around midway through the thread. This shows how to return an array, then maintain the values in the efs for future use as elements of an array declared globally using the unshift() and pop() array methods.
              Using Arrays

              here is how I would re-write your example:



              PHP Code:
              var = new Array(0,0,0,0); //length of 4 - this ensures when you unshift and pop that the m array is always of length 4.  This can be increased or decreased to however many you want.

              function main() {
              ....
              var 
              MyFunc1(parameters);
              m.unshift(X);
              m.pop();
              var 
              xx m[0][0];  //  where m[0][0],m[1][0],m[2][0],m[3][0], are the global values, or X[0], the local variable 
              var yy m[0][1];
              var 
              zz m[0][2];
              ...
              }

              function 
              MyFunc1(parameters) {
              ......
              return new Array(
              V0,V1,V2//Vx are calculated by this function

              The key to ensuring the data is available is by declaring the m variable outside any function, i.e. globally.

              Instead of returning an array, I find it more helpful to return named objects, this makes it easier to code IMHO. Here is the same example revised to return an object

              PHP Code:
              var = new Array(0,0,0,0,0,0,0,0); //length of 8 - this ensures when you unshift and pop that the m array is always of length 8.  This can be increased or decreased to however many you want.


              function main() {
              ....
              var 
              MyFunc1(parameters);
              m.unshift(X);
              m.pop();
              var 
              xx m[0].v0;  // where  m[0].v0,m[1].v0,m[2].v0,m[3].v0, are the global values, or X.v0, the local variable 
              var yy m[0].v1;
              var 
              zz m[0].v2;
              ...
              }

              function 
              MyFunc1(parameters) {
              ......
              //returning data as an object
              return { v0 V0v1 V1v2 V2}; //Vx are calculated by this function

              Comment


              • #8
                Steve,

                Thank you, but it is not what I was looking for.

                My values are already global, but are not organized in array. Moreover, I need to be able to re-group them, as needed.

                I tried to return an array, then look for them. Not only does not work, but computer goes in an infinite loop.

                I don't know if having the array as global var is a Jave requirement, but as far as I am concerned, this feature [returning usable arrays from internal functions, decalred in main] is not supported and has never been supported.

                Note: I provide a clear example below, because I don't want to get a [marketing] reply that defends eSignal, unless I am told what I am doing wrong [because I would love to be able to use this feature].

                Thank you

                //******
                In main [or even in other function, should be the same] I declare the array as follows:
                main() {
                var XX = new Array(0,0,);
                XX = MyFunction(parameters];
                var xx = XX[0];
                var yy = XX[1];
                }

                function MyFunction(parameters) {
                var x = 2;
                var y = 4;
                return new Array(2,4);
                }
                Mihai Buta

                Comment


                • #9
                  Originally posted by mbuta
                  Steve,

                  Thank you, but it is not what I was looking for.

                  My values are already global, but are not organized in array. Moreover, I need to be able to re-group them, as needed.

                  I tried to return an array, then look for them. Not only does not work, but computer goes in an infinite loop.

                  I don't know if having the array as global var is a Jave requirement, but as far as I am concerned, this feature [returning usable arrays from internal functions, decalred in main] is not supported and has never been supported.

                  Note: I provide a clear example below, because I don't want to get a [marketing] reply that defends eSignal, unless I am told what I am doing wrong [because I would love to be able to use this feature].

                  Thank you

                  //******
                  In main [or even in other function, should be the same] I declare the array as follows:
                  main() {
                  var XX = new Array(0,0,);
                  XX = MyFunction(parameters];
                  var xx = XX[0];
                  var yy = XX[1];
                  }

                  function MyFunction(parameters) {
                  var x = 2;
                  var y = 4;
                  return new Array(2,4);
                  }
                  Mihai,

                  Your code is fraught with errors, which explains why it is not working. I corrected all of your errors in the below listing and it works fine. I enclosed the corrected code below, along with a screen shot of the formula output window.

                  btw, this is not a marketing reply that defends eSignal, and based on your response you mean that in a derogatory manner. I also provided multiple examples with links to efs's where this functionality was used and clearly works, only to receive a reply of this nature from you.

                  I wish you would pay more attention to your errors in your code prior to making such posts.

                  PHP Code:
                  function main() {
                  var 
                  parameters 0;
                  var 
                  XX MyFunction(parameters);
                  var 
                  xx XX[0];
                  var 
                  yy XX[1];
                  debugPrintln ("7: xx = "+xx+" yy = "+yy);
                  }

                  function 
                  MyFunction(parameters) {
                   var 
                  2;
                   var 
                  4;
                   return new Array(
                  2,4);


                  Comment

                  Working...
                  X