Announcement

Collapse
No announcement yet.

More Fun with Multi-dimensional Arrays

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

  • More Fun with Multi-dimensional Arrays

    I have been working with a multi-dimensional array in my latest efs study and have hit a roadblock.

    After reading Jay's example at this link I got brave and tried it.

    Everything works OK until I need to expand the array beyond the limits I originally set up for the array. Since I don't know how many elements I'm going to need I can only think to set up an extremely large array, but it seems to me that there must be a better way.

    Using Jay's example of a two-dimensional with 4 rows (see below), how would you add a 5th row to the array after it has already been defined?

    Dale Sullivan



    PHP Code:

    myVar
    ="Multidimensional array test; "
    = new Array(4)
    for (
    i=04i++) {
       
    a[i] = new Array(4)
       for (
    j=04j++) {
          
    a[i][j] = "["+i+","+j+"]"
       
    }
    }
    for (
    i=04i++) {
       
    str "Row "+i+":"
       
    for (j=04j++) {
          
    str += a[i][j]
       }
       
    myVar += str +"; "


    Multidimensional array test;
    Row 0:[0,0][0,1][0,2][0,3];
    Row 1:[1,0][1,1][1,2][1,3];
    Row 2:[2,0][2,1][2,2][2,3];
    Row 3:[3,0][3,1][3,2][3,3];
    Last edited by tasman; 12-18-2003, 10:34 AM.

  • #2
    So....

    Create the array without any limits....

    You'll need to look up "push" and "pop" as far as array functions, but it can be done...

    You can look at my mouse play post. In that post, I create an array of objects for the lines, then populate the array when the user clicks on the chart. If the user Disables the lines for a bar, I simply deactivate the lines (leaving the array item for that bar in place). Then, if the user re-enables the lines for that bar, I find, then update the array data.

    Using the array functions and playing with it some more, you'll figure it out. If you need help, let me know.

    The example below shows you how to test for array length, then do something. You'll still need to ADD TO THE ARRAY with the push command.

    PHP Code:

    myVar
    ="Multidimensional array test";
    = new Array;

    if (
    a.length 0) {
     for (
    i=0a.lengthi++) {
       
    a[i] = new Array(4);
       for (
    j=04j++) {
          
    a[i][j] = "["+i+","+j+"]"
       
    }
     }
    }

    if (
    a.length 0) {
     for (
    i=0a.lengthi++) {
       
    str "Row "+i+":"
       
    for (j=04j++) {
          
    str += a[i][j]
       }
       
    myVar += str +"; "
     
    }

    Brad Matheny
    eSignal Solution Provider since 2000

    Comment


    • #3
      Thanks for the reply, Doji.

      After a lot of research, which didn't really help much at all, I finally figured it out. I found nothing that explains how to add a new row of elements to an existing multi-diemnsional array. When I tried to use the regular push statement to add 3 elements to the array it was converted to a single-dimension array and the 2-dimension access statements didn't work any more, causing an error message.

      Like most things in life, once you know how it's done it seems so obvious. The statement I had to use to add a new row was:

      ArrayName.push(new Array(3));

      Included below is a sample efs study I did to test it and a sample of the output it creates in the formula output window.

      It was an interesting adventure.

      Dale Sullivan



      PHP Code:
      var vOpenTrades = new Array();        // Array of open trades

      function preMain() {
          
      setStudyTitle("Array Test");
          
      setCursorLabelName("Array Test");
          
      setShowCursorLabel(false);
          
      setPriceStudy(true);

          
      //Create a 2 dimensional array - 3 elements in 3 rows
          
      vOpenTrades = new Array(3)
          for (
      i=03i++) {
              
      vOpenTrades[i] = new Array(3)
              for (
      j=03j++) {
                  
      vOpenTrades[i][j] = "["+i+","+j+"]"
              
      }
          }

          
      // Set all elements of the array = 0
          
      for (i=03i++) {
              for (
      j=03j++) {
                  
      vOpenTrades[i][j] = 0;
              }
          }
      }

      function 
      main() {

          
      nState getBarState();

          if(
      getCurrentBarIndex() == 0){
              if(
      nState == BARSTATE_NEWBAR){

                  
      // Add another row to the array
                  
      vOpenTrades.push(new Array(3));

                  
      // Put some data in the elements of the new row 
                  
      vOpenTrades[vOpenTrades.length 1][0] = vOpenTrades.length;
                  
      vOpenTrades[vOpenTrades.length 1][1] = vOpenTrades.length 1;
                  
      vOpenTrades[vOpenTrades.length 1][2] = vOpenTrades.length 2;

                  
      // Print the length of the array
                  
      debugPrintln("vOpenTrades.length = " vOpenTrades.length);

                  
      // Print the contents of the array
                  
      debugPrintln("vOpenTrades = " vOpenTrades);

                  
      // Print the first element of the new row
                  
      debugPrintln("vOpenTrades[vOpenTrades.length - 1][0] = " vOpenTrades[vOpenTrades.length 1][0]);

                  
      debugPrintln("");
              }
          }

      Attached Files

      Comment


      • #4
        Hi
        I'm having trouble with multidimensional array and would appreciate your help. I understand the 2d array but im trying to extend this to 3 dimensional array. Any kind of help would be much appreciated. i then need to recall the stored data.. an example would also be helpful

        Regards

        Kalpesh

        Comment

        Working...
        X