Detecting datatype from a string

 

Does anyone know a way to detect a datatype from a string value?

This can be important when reading from a CSV file, as we all know mql5 reads a csv file as a data of one type it, for example reading the information from a CSV as strings which works better and gives you the option to convert the data loaded from a file into whatever datatype you intend to, Just like using pandas.read_csv 

matrix ReadCsv(string file_name,string delimiter=",",bool common=false)
  {
   matrix mat_ = {};
   int rows_total=0;
   
   int handle = FileOpen(file_name,FILE_SHARE_READ|FILE_CSV|FILE_ANSI|(common?FILE_COMMON:FILE_ANSI),delimiter);
   
   if(handle == INVALID_HANDLE)
     {
      printf("Invalid %s handle Error %d ",file_name,GetLastError());
      Print(GetLastError()==0?" TIP | File Might be in use Somewhere else or in another Directory":"");
     }

   else
     {
      int column = 0, rows=0;

      while(!FileIsEnding(handle) && !IsStopped())
        {
         string data = FileReadString(handle); 
         
         //---
         
         if(rows ==0)
           {
            ArrayResize(csv_header,column+1);
            csv_header[column] = data;
           }
         
         
         if(rows>0)  //Avoid the first column which contains the column's header
          {
              mat_[rows-1,column] = (double(data));
          }
         
         
         column++;

         //---

         if(FileIsLineEnding(handle))
           {
            rows++;

            mat_.Resize(rows,column);
            
            column = 0;
           }
        }

      rows_total = rows;

      FileClose(handle);
     }
   
   Comment("");
   
   mat_.Resize(rows_total-1,mat_.Cols());

   return(mat_);
  }


I would like to be able to detect the datatype from a string which could have data in different formats like: 

Something like this,

enum dt_type
  {
   DT_DOUBLE_FLOAT_INT,
   DT_DATETIME,
   DT_STRING
  };

dt_type DetectType(string val)
 {
   //if (StringToDouble(val))
   //How can one detect a data type here ???
   
   return DT_DATETIME; //In case datetime was detected
 }
pandas.read_csv#
  • pandas.pydata.org
, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , # Read a comma-separated values (csv) file into DataFrame. Also supports optionally iterating or breaking of the file into chunks. Additional help can be found in the online docs for IO Tools. Parameters filepath_or_buffer Any valid string path is...
 
Omega J Msigwa:
//if (StringToDouble(val))    //How can one detect a data type here ???

The problem I found with StringToDouble() is that it will return 0.0 for incorrect strings - no easy way of knowing whether it is pass/fail.

For example:

      string test = "AB.12";
      
      double result = StringToDouble(test);
      
      PrintFormat("%s = %f", test, result);

Outputs:

AB.12 = 0.000000

So I think you need to write your own utils to check the strings in detail - I have done that, not just in MQL5 but other languages too whenever I find the standard tools omit something I need. 

This thread has some good examples which may help:

https://www.mql5.com/en/forum/151881

How to do "IsNumber"?
How to do "IsNumber"?
  • 2014.06.09
  • www.mql5.com
Hi! I'd like test whether the OrderComment is a convertible from string to a number or not, but I can't find anything like an IsNumber() function...
 
R4tna C #:

Yeah, not to mention converting a string "0.0", will result into 0.0 just like converting "AB.12" leads to 0

 
Omega J Msigwa:

Does anyone know a way to detect a datatype from a string value?

This can be important when reading from a CSV file, as we all know mql5 reads a csv file as a data of one type it, for example reading the information from a CSV as strings which works better and gives you the option to convert the data loaded from a file into whatever datatype you intend to, Just like using pandas.read_csv 


I would like to be able to detect the datatype from a string which could have data in different formats like: 

Something like this,

You can use MqlParam to detect the datatype from a string.
I just found an example for reading file: https://www.mql5.com/en/code/24777