Create a relationship between 2 Arrays - Most efficient way?

 

Hi All,

Is there any efficient way to create a relationship between two (or more) Arrays that contain different data types?  

For example, I have an Array of data type string that contains several pairs (e.g. AUDUSD, EURUSD, EURGBP etc) and another array of data type int that contains the Magic Numbers that need to be associated to each pair (e.g. 1000, 2000, 3000 etc), so that AUDUSD is always associated with 1000, EURUSD with 2000 an so on. A sort of what happens in a relational database.

The solution that I have found so far is create 2 arrays of the same size and sort them in a way that the index corresponds to the combination I want:

string CrossesArray[]={"AUDUSD","EURUSD","EURGBP"};

int MagigNumbersArray[]={1000,2000,3000};

However this solution is quite weak and prone to errors. I have looked at structures, but that does not seem to help.

Any other ideas?

Thanks,

MG

 

Hi,

So you could do as array of structure as simple as below:

struct pair_magic
  {
   string            Pairs;
   int               MagicNumbers;
  };
//+------------------------------------------------------------------+
int OnInit()
  {
   pair_magic Pair_Magic_Array[]={{"AUDUSD",1000},{"EURUSD",2000},{"EURGBP",3000}};

   Print(Pair_Magic_Array[0].Pairs+" | "+Pair_Magic_Array[0].MagicNumbers);
   //result will be : AUDUSD | 1000

   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
void OnTick()
  {

   return;
  }
//+------------------------------------------------------------------+
Regards.
 

Awesome, I'll try that.


Thanks Mehrdad,

MG

 
MacGiamma:

Awesome, I'll try that.


Thanks Mehrdad,

MG

You're welcome,