Hello
I'm trying to separate dates for statistical analysis. I have two dates arrays, I want to separate dates that are in Array2 but not in Array1.
I think my code compare each array's index by index.
How can I take Array2 index then loop the whole Array1 to find a match and if there's non, then save this index?
Your code appears to be appending too often to the results array, so you end up with repeating values and a large dataset. Better if you separate the array append step so it is outside the inner Array2 loop and only executed once after the array scanning is complete.
Here is a code example based on your code, which I hope helps
//+------------------------------------------------------------------+ //| 427908-ArrayCompare.mq5 | //| Copyright 2022, MetaQuotes Ltd. | //| https://www.mql5.com | //+------------------------------------------------------------------+ #property copyright "Copyright 2022, MetaQuotes Ltd." #property link "https://www.mql5.com" #property version "1.00" //+------------------------------------------------------------------+ //| Script program start function | //+------------------------------------------------------------------+ string results[] = {}; int coo = 0; string Array1[] = {"2012.01.12", "2012.01.13", "2012.01.20", "2012.01.24", "2012.01.25", "2012.01.26", "2012.01.31", }; string Array2[] = {"2012.01.11", "2012.01.13", "2012.01.13", "2012.01.18", "2012.01.20", "2012.01.20", "2012.01.21", "2012.01.21", "2012.01.25", "2012.01.26", "2012.01.27", "2012.01.27", "2012.01.28", }; //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void OnStart() { bool match = false; for(int i = 0; i < ArraySize(Array1); i++) { match = false; for(int ii = 0; ii < ArraySize(Array2); ii++) { if(Array1[i] == Array2[ii]) { match = true; break; //No need to scan Array2 any further } } //end - Array 2 loop if(!match) { ArrayResize(results, coo + 1); results[coo] = Array1[i]; coo++; } } //end - Array 1 loop ArrayPrint(Array1); ArrayPrint(Array2); ArrayPrint(results); }//+------------------------------------------------------------------+
Your code appears to be appending too often to the results array, so you end up with repeating values and a large dataset. Better if you separate the array append step so it is outside the inner Array2 loop and only executed once after the array scanning is complete.
Here is a code example based on your code, which I hope helps
Thank you very much!
I had already done it that way, this was going to help!
for(int i = 0; i < ArraySize(Array2); i++) { bool match = false; for(int ii = 0; ii < ArraySize(Array1); ii++) if(Array2[i] == Array1[ii]) match = true; if(!match) { ArrayResize(results, coo + 1); string a="~"; string b= Array2[i]; string c="~,"; results[coo] = a+b+c; coo++; } }
Ah yes Excel is good - if you want the code to do it for you I realized you can add a filter - am posting just the relevant section only:
//Global Variables needed: string duplicateFilter = ""; string separatorStr = ";"; ushort separatorUsh = StringGetCharacter(separatorStr, 0); //Code change if((!match) && (StringFind(duplicateFilter, Array1[i]) == -1)) { ArrayResize(results, coo + 1); results[coo] = Array1[i]; coo++; duplicateFilter += (StringLen(duplicateFilter) > 0)? separatorStr + Array1[i] : Array1[i]; }
It is a really easy way to avoid duplicates (assuming your data sets are not too large to be handled by strings).
And I later realised this also leads to a quick way to populate the results array at the end instead of resizing the results array on each iteraton
StringSplit(duplicateFilter, separatorUsh, results);
Ah yes Excel is good - if you want the code to do it for you I realized you can add a filter - am posting just the relevant section only:
It is a really easy way to avoid duplicates (assuming your data sets are not too large to be handled by strings).
And I later realised this also leads to a quick way to populate the results array at the end instead of resizing the results array on each iteraton
Great Stuff!! I know this will help a lot of people in future!
Be Blessed!
I purchased the Trade Manager but the download shows mt5setup. What should I do?
Two blog posts with instruction about HowTo:

- www.mql5.com

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hello
I'm trying to separate dates for statistical analysis. I have two dates arrays, I want to separate dates that are in Array2 but not in Array1.
I think my code compare each array's index by index.
How can I take Array2 index then loop the whole Array1 to find a match and if there's non, then save this index?