Announcement

Collapse
No announcement yet.

Arrays

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

  • Arrays

    if I have a statement

    return new Array( var1, var2);

    and I want to refer to var1(-3), what do I have to do?

    define a var var1=new Array(4); ?

  • #2
    Hello dloomis,

    You're on the right track. Take a look at CustomFuncRef.efs here. This is the method you're looking for.
    Jason K.
    Project Manager
    eSignal - an Interactive Data company

    EFS KnowledgeBase
    JavaScript for EFS Video Series
    EFS Beginner Tutorial Series
    EFS Glossary
    Custom EFS Development Policy

    New User Orientation

    Comment


    • #3
      Keeping an array of the return values that you push() and shift() (or unshift() and pop()) is certainly one way to go.

      You can also use ref(-x), which will return the return values x bars ago. Ref does seem to understand multiple element returns, so this is an option as well.

      I have a gut level feeling that using arrays will be less overhead/faster - but I have never really done any testing on this.

      Garth
      Garth

      Comment


      • #4
        Thanks for the pointers.

        This will make you cringe, but here is what I decided to do, cuz I understand it.

        if(getBarState==BARSTAT_NEWBAR){
        var1_Old=var;
        var1_Older=var1_Old;
        }

        then I calculate the current value of var1.

        Comment


        • #5
          David,

          That works, and is the way I handle it when I only want to remember one or two elemets (yes I would even create var2_Old). For more than that I would use arrays...

          Look at some of Chris's EFS studies for some great examples on using arrays.

          Garth
          Garth

          Comment


          • #6
            Dave,

            Here is a file I wrote which deals with arrays which will help in understanding array manipulation and declaration. As always there are twenty ways to skin the cat and as Garth indicated, what you are doing works. I commented it pretty well so it should be self explanatory.





            PHP Code:

            //declare before preMain
            var nFirstArray = new Array;
            var 
            nSecondArray;
            var 
            barcount 0;

            function 
            preMain(){ //start of preMain
                //two ways to do this, initialize an array here, or call a function to initialize an array, either way works
                //initializing an array like this is required if you are going to unshift and pop an array and have it remain 
                // as 10 records long(in this example).  
                //here is the first way
                
            var i;
                for (
            0;10i++){
                    
            nFirstArray[i] = 0;
                }
                
            //here one example of using a function, although the declaration is a little redundant
                //nFirstArray = initializeArrays(); 
                //here is a second example of a global array using a function
                
            nSecondArray initializeArrays(); //notice this was only defined as a global variable, this step makes it a global array
            }//end of preMain

            function main(){  // start of main

                
            var BarState getBarState();
              var 
            nIndex getCurrentBarIndex();    //debugPrintln("nIndex = " +nIndex); 
                
                
            if (BarState == BARSTATE_NEWBAR) {  
                    
            barcount +=1;
                        
                    
            //here is a third example of a local array using a function
                    
            var nThirdArray;
                    
            nThirdArray initializeArrays(); //takes on properties and values or returning array
                    //now lets say you have a function that returns a value or an array and you want to save them to a global variable/array
                        
                    
            var g1 data1(barcount);//I'll use the third method to get the array data
                    
            nFirstArray.unshift(g1);//puts new returning array as element [0] of nFirstArray
                    //where nFirstArray[0][0] = ndata1[0] and nFirstArray[0][1] = previous ndata1[0] and nFirstArray[0][2] = one before previous ndata1[0] 
                    //where nFirstArray[0][1] = ndata1[1] ...etc
                    //where nFirstArray[0][2] = ndata1[2] ...etc
                    
                    //when you unshifted nFirstArray, you made it 11 elements long, you could continue and save 
                    //all of them, but this would use up ,memory and slow things down, so get rid of the 11th element by pop'ing it off
                    
            nFirstArray.pop;//this keeps the array 10 records long (10 x 3)
                    
                    //if you want you could use this data, but it is 11 bars or so old
                    //var old = nFirstArray.pop 
                    //print out last 3 records upon reload
                    
            if (nIndex > -3){
                        
                        
            debugPrintln("######################");
                        
            debugPrintln("nIndex = " +nIndex+"  most recent nFirstArray = "+nFirstArray[0]); 
                        
            debugPrintln("nIndex = " +nIndex+"  one prior   nFirstArray = "+nFirstArray[1]); 
                        
            debugPrintln("nIndex = " +nIndex+"  two prior   nFirstArray = "+nFirstArray[2]); 
                        
            debugPrintln("nIndex = " +nIndex+"  three prior nFirstArray = "+nFirstArray[3]); 
                        
            debugPrintln("nIndex = " +nIndex+"  four prior  nFirstArray = "+nFirstArray[4]); 
                        
            debugPrintln("");
                    }
                }
                return;    
            }
            //end main

            function data1(num){
                var 
            ndata1 = new Array;
                var 
            var0 num 5;
                
            ndata1[0] = num 5;
                
            debugPrintln("ndata1[0] = " +ndata1[0]); 
                
                var 
            var1 num +65;
                
            //ndata1[1]= num + 65;
                
            var var2 num +17;
                
            //ndata1[2]= num + 17;
                
                
            ndata1[0] = var0;
                
            debugPrintln("var0 = " +var0); 
                
            ndata1[1] = var1;
                
            ndata1[2] = var2;
                
                return 
            ndata1;//this returns ndata1 which is defined locally here as an array
                //this is equivalent to return new Array (var0,var1,var2);
            }
                

            function 
            initializeArrays(){
                var 
            i;
                var 
            nSample = new Array();
                for (
            0;10i++){
                    
            nSample[i] = 0;
                }
                return 
            nSample;

            Attached Files

            Comment


            • #7
              FWIW, It had not occured to me and thus did not state such, but the efs in the post below requires that the formula output window be open to observe the efs output.

              Comment

              Working...
              X