stevehopwoodforex.com
https://www.stevehopwoodforex.com/phpBB3/
Print view

Pass array name to function?
https://www.stevehopwoodforex.com/phpBB3/viewtopic.php?t=2967
Page 1 of 1
Author:  Radar [ Sat Aug 10, 2013 10:37 am ]
Post subject:  Pass array name to function?

Hey gang,

I have 3 functions to backup, restore, and trim arrays, but at the moment, they all only work with a single array.

Here's my TrimArray function...

Code: Select all

void TrimArray()
{
   for(int i=(ArrayRange(MyTf,0)-1); i>=0; i--)
   {
      EmptyElements=MyTf[i,0];
      if(EmptyElements==0) a++;
   }
   ArraySort(MyTf,WHOLE_ARRAY,0,MODE_DESCEND);
   int CurrentArrayRange=ArrayRange(MyTf,0);
   Alert("Array Size is now ", CurrentArrayRange);
   int NewArrayRange=CurrentArrayRange-a;
   ArrayResize(MyTf, NewArrayRange);
   CurrentArrayRange=ArrayRange(MyTf,0);
   Alert("Array Size is now ", CurrentArrayRange);
   ArraySort(MyTf);
} // End TrimArray
I only learnt how to do the basics with arrays 2 nights ago, so I've been throwing alerts as often as possible, so that I can see where I stuff up ;)

I've tried changing the function to accept the array name as a string, (void TrimArray(string ArrayToTrim)), but execution never gets past the for(int i=(ArrayRange(ArrayToTrim,0)-1); i>=0; i--) line of code :(

So, as the title says, is there any way to pass the array name to the function, and replace the instances of MyTf with variable names so that one set of code suits all, or do I have to repeat the code for each array that I want to trim, (or backup/restore, as the case may be)?

I'm most definitely at the limit of my knowledge with this, and really need an example of how to do it, if it's possible to do at all.

Thanks in advance :)

Have fun!

Radar =8^)
Author:  dietcoke [ Sat Aug 10, 2013 11:48 am ]
Post subject:  Re: Pass array name to function?

Radar wrote:Hey gang,

I have 3 functions to backup, restore, and trim arrays, but at the moment, they all only work with a single array.

I only learnt how to do the basics with arrays 2 nights ago, so I've been throwing alerts as often as possible, so that I can see where I stuff up ;)

I've tried changing the function to accept the array name as a string, (void TrimArray(string ArrayToTrim)), but execution never gets past the for(int i=(ArrayRange(ArrayToTrim,0)-1); i>=0; i--) line of code :(

So, as the title says, is there any way to pass the array name to the function, and replace the instances of MyTf with variable names so that one set of code suits all, or do I have to repeat the code for each array that I want to trim, (or backup/restore, as the case may be)?

I'm most definitely at the limit of my knowledge with this, and really need an example of how to do it, if it's possible to do at all.

Thanks in advance :)

Have fun!

Radar =8^)
You cannot pass an array as a parameter to a function. What you have to do is pass a reference to the array instead. Decent explanation here: http://en.fortrader.ru/mql/nedokumentir ... -mql4.html

Any way, the syntax is thus:

Code: Select all

// where MyArray is a reference to an integer array
void TrimArray(int& MyArray) 
{
   for(int i=(ArrayRange(MyArray,0)-1); i>=0; i--)
   {
      EmptyElements=MyArray[i,0];
      if(EmptyElements==0) a++;
   }
   ArraySort(MyArray,WHOLE_ARRAY,0,MODE_DESCEND);
   int CurrentArrayRange=ArrayRange(MyArray,0);
   Alert("Array Size is now ", CurrentArrayRange);
   int NewArrayRange=CurrentArrayRange-a;
   ArrayResize(MyArray, NewArrayRange);
   CurrentArrayRange=ArrayRange(MyArray,0);
   Alert("Array Size is now ", CurrentArrayRange);
   ArraySort(MyArray);
} // End TrimArray
You call it like this

TrimArray(MyArrayToTrim);

where MyArrayToSort is the array you want to trim.
Author:  Radar [ Sat Aug 10, 2013 12:13 pm ]
Post subject:  Re: Pass array name to function?

Hey Dietcoke,

Thanks for that!! :)

It took me 5 hours to just get the basics into my head, and another 12 hours to get from there to here.

I'm off to put this into practise until it sticks :D

Have fun!

Radar =8^)
All times are UTC Page 1 of 1