# Convert a string to a variable

12673

Hi guys

I am writing an indicator where the user defines which currencies to be used

extern string               Currency1="EURUSD";
extern string               Currency2="GBPUSD";
extern string               Currency3="USDCHF";

.

.

.

extern string               Currency13="EURNZD";

then these currency names are stored into an array

currencyString[0]=Currency1;

.

.

.

currencyString[11]=Currency12;

currencyString[12]=Currency13;

my question is rather than assigning values to the array one by one is there a way to apply a loop to do that, given the variable names are common

for example

for (int i=0; i<13; i++){

currencyString[i]=Currency +(i+1);

}

the above of course will return an error as Currency is not defined.

If there is a way to concatenate text ("Currency" +i)  and then look if there is a variable that has that name problem is solved.

Thank you

Moderator
5679

Michalis Phylactou:

Hi guys

I am writing an indicator where the user defines which currencies to be used

extern string               Currency1="EURUSD";
extern string               Currency2="GBPUSD";
extern string               Currency3="USDCHF";

.

.

.

extern string               Currency13="EURNZD";

then these currency names are stored into an array

currencyString[0]=Currency1;

.

.

.

currencyString[11]=Currency12;

currencyString[12]=Currency13;

my question is rather than assigning values to the array one by one is there a way to apply a loop to do that, given the variable names are common

for example

for (int i=0; i<13; i++){

currencyString[i]=Currency +(i+1);

}

the above of course will return an error as Currency is not defined.

If there is a way to concatenate text ("Currency" +i)  and then look if there is a variable that has that name problem is solved.

Thank you

```for (int i=0; i<SymbolsTotal(1); i++){

currencyString[i]=SymbolName(i,1);

}```
```for (int i=0; i<SymbolsTotal(1); i++){

currencyString[i]=SymbolName(i,1)+IntegerToString(i+1));

}```
12673

Marco vd Heijden:

Thank you

It may be able to work, but only because there is a function in MQL5 that looks the order of symbols in the market watch.

However I wish to know if there is a way to write a string and see if there is a variable with that string and return its value.

Moderator
5679

Michalis Phylactou:

Thank you

It may be able to work, but only because there is a function in MQL5 that looks the order of symbols in the market watch.

However I wish to know if there is a way to write a string and see if there is a variable with that string and return its value.

`StringFind()`
12673

Marco vd Heijden:

StringFind() searches to see if a substring exists within a string.

What I want is to specify a string, see if a variable exists with that string and return its value

Moderator
5679

I dont get it.

The string is the variable ? what more can it return but it's name/position ?

Then you simply loop over

`WHOLE_ARRAY`

And if it's a match you know it's location.

24175

Answer to OP question is NO but here is one other idea:

`extern string               DCDCurrencies="EURUSD,,USDJPY,,GBPUSD"; //then you can loop the string segments in to an array`
Personally I'd just go with your original code though.
24175

Marco vd Heijden:

I dont get it.

The string is the variable ? what more can it return but it's name/position ?

Then you simply loop over

And if it's a match you know it's location.

I'm sure he means "...string NAME and return it's value." Like what he's asked in his original post.
12673

Benjamin Dixon:

Answer to OP question is NO but here is one other idea:

Personally I'd just go with your original code though.

Thank you Benjamin

Is not very user friendly however.

I just wanted to know if it can be done , for educational purposes and cleaning up the code, as I get faced with this quite often.