Array displays "" on the first round of the loop instead of the value "00"

 

I cannot get this array to display the values in order the first time the loop is run. It is supposed to display

"0 cnt str 00"

"1 cnt str 11"

for the initial go round but instead it prints "0 cnt str " with no value for the arr[0] Then on the second round it seems to act as expected. I read some on around on arrays about ArrayInitialize() and ArrayResize(). Still I cannot figure out what I have done wrong.

Thanks!

#property copyright "Copyright © 2011, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"

#property indicator_chart_window
//--- input parameters
extern string       ArrayStr="00,11,22,33,44,55555";


//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int    counted_bars=IndicatorCounted(),
          cnt=0; 
   string arrstr[0];
              
 // Print(cnt+" 1st cnt and int ", arrstr[cnt]);
  load_array(ArrayStr, arrstr);
  cnt=ArraySize(arrstr);
  Print(cnt+"returned value");
  for(int i=0;i<cnt;i++){ Print(cnt+" cnt and int ", arrstr[cnt]);}
  //I probably need to add a line of code here something like string arrstr[]; or ArrayInitialize(arrstr,0) 
  // or even ArraySize(arrstr[],0);
  
//----
   return(0);
  }
  
int load_array(string str,string& arr[])
{
   string     del=",",
              seg="";
               
                
      int ind=0,
          pos1,
          pos2;
        
   
    
    pos2=StringFind(str,del);
    seg=StringSubstr(str,pos1,pos2-pos1);
    ArrayResize(arr,ArraySize(arr)+1);
    arr[ind]=seg;
    //Print(arrtfs[ind]+"seg");
    ind++;
     while(pos2>0)
   {    // Print("index ",ind+" "+seg);
        pos1=pos2;
        pos2=StringFind(str,del,pos2+1);
        if(pos2==-1){ seg=StringSubstr(str,pos1+1,StringLen(str)-pos1);}
        else{ seg=StringSubstr(str,pos1+1,pos2-pos1-1);}
       // Print(seg);
        ArrayResize(arr,ArraySize(arr)+1);
        arr[ind]=seg;
         Print(ind+" cnt and int ", arr[ind]);
        ind++; 
      
   }
  
  
}
 

Please use this to post code . . . it makes it easier to read.

 

Unless I'm missing something . . .

cnt=load_array(ArrayStr, arrstr);

. . . doesn't work as you don't return a value from load_array despite declaring it type int.

 

Sorry about the format of the original post.

Thanks for pointing that out RaptorUK.

I read on your forums how to do function call that assigns values to an array using &. And thought maybe this might help me bypass my original problem which was the array growing by the amount of elements each time the start() function is called. Though I learned about the reference call I am back with the original problem.

How do I reset the array to array[] or zero elements in the array. Not only zeroing out the elements of it.

If you could point me in the right direction with a post or tutorial that would be great also. Read an article by coderguru on Arrays but it this wasn't covered in its scope.

Thanks again.

 
fractalcurve:

Sorry about the format of the original post.

Thanks for pointing that out RaptorUK.


How do I reset the array to array[] or zero elements in the array. Not only zeroing out the elements of it.

You are welcome.

Why do you need to reset the size of the array ? if ArrayResize (array, 0) doesn't work why can't you just set it to 1 and ignore the first entry ?

 


Sorry that it took so long to reply, I meant to earlier and then was sidetracked. The reason that i would like to learn how to zero out the Array in its entirety is in the event I would like to figure or manipulate a string(in this case) dynamically i would most likely use an Array for each segment whether delimited by spaces commas or others. Then the Array would be processed accordingly and reinvented with the next run of the mandating loop.

In this example I know the code has its limits those being the string used is set and cannot be changed with out running the deint() and init() built-in functions. But the purpose is exactly the same. I am obviously missing remedial knowledgeof the way array functions and Arrays are used; Resetting and Array, Resizing an Array and And accessing the contents of that Array. There are times in the past when I used Arrays in different languages. Visual Basic for instance. Where split would have been used for this function. And a for each loop would have cyclyed through the elements. If you could point me in the direction of an article post here on the forum or tutorial somewhere that explains Array use in depth rather than a brief touch on the differences in dimensions and defining Arrays in general. That would be more than helpful. Have done searches myself and they come back with info explaining what Arrays are and not diving in as I need.

Coderguru had an article that was right up my alley and the I when I reached the end I was disappointment since apparently the continuance was never issued.

Since reading the replies I added the line ArrayResize(arrstr,0); forgetting I tried it once before and the results were different but also wrong.

the results returned begin at

"1 cnt and str 11"

and then end at

"5 cnt and str5555"

then "6 cnt and str" with a blank. not "00"

is printed six times in a row.

I am eager to get to the bottom of this and learn how to avoid out of order manipulations and processing.

Thanks once again for your time:)

 

sorry, stupid mistake. Used cnt for the index of the arrstr[] Array and not the increment int i. Usually they are stupid mistakes like that aren't they.

So if any one need to know how to reset a string Array. The answer is ArrayResize(arrstr,0); Then of course add 1 with ArrayResize(arrstr,ArraySize(arrstr)+1); when loading it up again.

and also a thing to note is cnt

cnt=ArraySize(arrstr);

is the actual count in the zero based array. If there are 5 elements in the array, 0-4. then 5 is returned not 4 . And the loop to manipulate

the elements would bet

for(int i;i<cnt;i++)

//and not

for(int i;i<cnt+1;i++)


I'm sure no one else has run into this problem. Not sure why I did.

Thanks again for the help.

Reason: