Announcement

Collapse
No announcement yet.

problems with arrays

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

  • problems with arrays

    Hi Guys,

    I am having problems with arrays, I know where the problem is but I don't understand why this is happening.

    As an example, I wrote this little study:

    function preMain() setPriceStudy(false);

    var myarray = new Array();
    var ii = null;

    function main() {

    nTime = getValue("rawtime",0); //get current time value

    barnday = Math.round(getCurrentBarIndex()-getFirstBarIndexOfDay(nTime)); // get the current daily bar index (first bar of the day = 0, second = 1, etc.)

    if(barnday == null) return;

    if(barnday==0) ii=0; //if this is the first bar of the day, ii = 0

    dim=myarray.push(2*ii); //I add the element 2*ii to myarray. The dimension of myarray is stored in dim.
    ii++; //I increase ii by 1.

    return new Array( ii, dim);
    }

    This works very well. The values for ii reset at the beginning of each day, while the dim keeps growing.
    Now, say that I want to kill the array at the beginning of every new day. I try to do that and it doesn't work.
    If I try to CREATE a new array within the "if" statement, i.e., If modify the if statement in the code above like this:

    if(barnday==0) {
    var myarray = new Array();
    ii=0; //if this is the first bar of the day, ii = 0
    }

    I get the error "TypeError: myarray has no properties",

    It doesn't let me create an array in a conditional statement and call it later! Is it worried that I might call it before even creating it? I just want to destroy it and start a new one everyday. So, my two questions: a) Any ideas of why this is happening and b) what I can do to resolve my problem?
    Thanks a lot!

    Silvia.
    Last edited by sil; 01-17-2008, 05:19 AM.

  • #2
    solution to b)

    Hi there again

    I found a way round to resetting the array by using the pop() method, it is lovely. Still, if someone out there has some idea about why I can't do such a thing as described before, I am still very interested! Thanks a lot,
    Silvia.

    Comment


    • #3
      Re: problems with arrays

      Silvia
      The problem you are seeing is caused by the fact that in "killing" (to use your own convention) the array you are declaring it locally by using var myarray = new Array(); in this segment of code
      if(barnday==0) {
      var myarray = new Array();
      ii=0; //if this is the first bar of the day, ii = 0
      }

      On the first bar of the chart barnday will be equal to 0 hence myarray gets declared as a local variable. On the following bar the condition if(barnday == 0) no longer evaluates to true so the array does not get created [and does not exist because it was declared locally at the prior execution] thereby returning the error.
      To resolve this you just need to remove the var when re-initializing the array ie
      if(barnday==0) {
      myarray = new Array();
      ii=0; //if this is the first bar of the day, ii = 0
      }

      Once you make this change your script should work as expected
      Hope this helps
      Alex


      Originally posted by sil
      Hi Guys,

      I am having problems with arrays, I know where the problem is but I don't understand why this is happening.

      As an example, I wrote this little study:

      function preMain() setPriceStudy(false);

      var myarray = new Array();
      var ii = null;

      function main() {

      nTime = getValue("rawtime",0); //get current time value

      barnday = Math.round(getCurrentBarIndex()-getFirstBarIndexOfDay(nTime)); // get the current daily bar index (first bar of the day = 0, second = 1, etc.)

      if(barnday == null) return;

      if(barnday==0) ii=0; //if this is the first bar of the day, ii = 0

      dim=myarray.push(2*ii); //I add the element 2*ii to myarray. The dimension of myarray is stored in dim.
      ii++; //I increase ii by 1.

      return new Array( ii, dim);
      }

      This works very well. The values for ii reset at the beginning of each day, while the dim keeps growing.
      Now, say that I want to kill the array at the beginning of every new day. I try to do that and it doesn't work.
      If I try to CREATE a new array within the "if" statement, i.e., If modify the if statement in the code above like this:

      if(barnday==0) {
      var myarray = new Array();
      ii=0; //if this is the first bar of the day, ii = 0
      }

      I get the error "TypeError: myarray has no properties",

      It doesn't let me create an array in a conditional statement and call it later! Is it worried that I might call it before even creating it? I just want to destroy it and start a new one everyday. So, my two questions: a) Any ideas of why this is happening and b) what I can do to resolve my problem?
      Thanks a lot!

      Silvia.

      Comment

      Working...
      X