Download MetaTrader 5
To add comments, please log in or register
Find out how your rating can be improved
lostbridge
70
lostbridge 2014.04.12 00:19 

I want to return a number based on the content of a string.

int aaa=3;

int bbb=5;

string mypick="aaa";

For example:

if mypick="aaa", then I want return the value of aaa (aaa=3 in my example, so that the returned value should 3)

if mypick="bbb", then I want return the value of bbb

I google the forum but still don't know how to do that. Need your help. Thanks

Keith Watford
Moderator
7980
Keith Watford 2014.04.12 00:44  
int AnotherVariable;
if(mypick=="aaa")
  AnotherVariable=aaa;
if(mypick=="bbb")
  AnotherVariable=bbb;
   
James Hodges
2792
James Hodges 2014.04.12 00:47  

Hmmm why are you using a string? aaa is an integer not a string.... It's the NAME of the variable .. not the VALUE of the variable.. The VALUE is 3.

Just say

int mypick =aaa;//which means mypick is equal to the value that the variable named aaa is holding..

now if you need it to be a string so that it will print you can say..
Print( "The value of mypick is: ",aaa);

or

Print("The value of aaa is: ",aaa);

when you say aaa it prints the VALUE of that variable named aaa.. 3

when you say "aaa" it prints aaa......

PipPip...Jimdandy

Keith Watford
Moderator
7980
Keith Watford 2014.04.12 01:47  

You can do it in this roundabout way

   string ss="aaa,3,bbb,5,a,1,b,2,c,3,d,4,e,5,f,6,g,7,h,8,i,9,j,10,k,11,l,12,m,13,n,14,o,15,p,16,q,17,r,18,s,19,t,20,u,21,v,22,w,23,x,24,y,25,z,26";
   string ssArray[];
   StringSplit(ss,44,ssArray);
   int ssarraysize=ArraySize(ssArray);
   string mypick="aaa";
   int ret=0;
   for (int x=0;x<ssarraysize-1;x++)
      {
      if(ssArray[x]==mypick)
         {
         ret=StrToInteger(ssArray[x+1]);
         Print("mypick ",mypick,"=",ret);
         break;
         }
       if(x==ssarraysize-2)
         Print("mypick ",mypick,"=NO VALUE");
       }
Ian Venner
2583
Ian Venner 2014.04.12 02:54  

You could use defines

  #define aaa 3
  #define bbb 5
   
  Print(aaa);
  Print(bbb);

or create some enums

  enum vals
  {
   aaa=3,
   bbb=5
  };

  Print(aaa);
  Print(bbb);
Tjipke de Vries
6281
Tjipke de Vries 2014.04.12 04:37  
lostbridge:

I want to return a number based on the content of a string.


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+   
int StringtoNumber(string mypick) {
 switch(mypick) {
  case aaa     : return(3);  break;
  case bbb     : return(5);  break;
  case ...     : return(.);  break;
  case ...     : return(.);  break;
 }
 return(-1);//not defined
}


//int mypickup = StringtoNumber("aaa");

many ways

lostbridge
70
lostbridge 2014.04.12 05:47  
int StringtoNumber(string mypick) {
 switch(mypick) {
  case "aaa"     : return(aaa);  break;
  case "bbb"     : return(bbb);  break;
  case ...     : return(.);  break;
  case ...     : return(.);  break;
 }
 return(-1);//not defined
}
I dont have a better way but write cases one by one.
whroeder1
13640
whroeder1 2014.04.12 12:19  
lostbridge: I want to return a number based on the content of a string.

int aaa=3;

int bbb=5;

string mypick="aaa";

  1. Your last post
  2. An associative structure
    struct keyedInt{ string key; int val; }; keyedInt values[];
    void keyedIntInsert(string key, int val){
      int n = ArrayRange(values,0); ArrayResize(values, n+1);
       values[n].key = key, values[n].val = val;
    }
    int StringtoNumber(string mypick){
      for(int idx = ArrayRange(values,0)-1; idx >= 0; idx--)
        if(values[idx].key = mypick) return( values[idx].val );
      return(EMPTY);
    }
    OnInit(){
       keyedIntInsert("aaa", 3);
       keyedIntInsert("bbb", 5);
    :
       int val = StringtoNumber("bbb"); // 5
    or define a associative type class (see MQL4/include/Arrays/ArrayInt.mqh
  3. Don't use a strings at all.
    Constant values
    non-constant
    enum keys{ aaa, bbb, ...};
    int vals[] = { 3, 5, ... }
    
    
    :
       int value = vals[bbb];
    enum keys{ aaa, bbb, ..., nKeys};
    int vals[nKeys];
    On...(){
       vals[aaa] = 3; valus[bbb] = 5;
    :
       int value = vals[bbb]; // 5

/
To add comments, please log in or register