Create a relationship between 2 Arrays - Most efficient way?

To add comments, please log in or register
MacGiamma
73
MacGiamma  

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

Mehrdad Jeddi
29391
Mehrdad Jeddi  

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.
MacGiamma
73
MacGiamma  

Awesome, I'll try that.


Thanks Mehrdad,

MG

Mehrdad Jeddi
29391
Mehrdad Jeddi  
MacGiamma:

Awesome, I'll try that.


Thanks Mehrdad,

MG

You're welcome,
To add comments, please log in or register