Looking for sscanf() function in MQL?

To add comments, please log in or register
Shui Wang
170
Shui Wang  

Hi,

I'm looking for a function that can retrieve numeric values from a formatted string.

MQL has functions like printf(), StringFormat, etc, to format values to string, but I just can not find a "formatted input" function, i.e., like C's "sscanf()".

Anyone has a clue? Thanks!

Shui Wang
170
Shui Wang  
This requirement is specially important when you want to change a parameter AFTER the EA has been running.
honest_knave
Moderator
2308
honest_knave  

The most simple way is a typecast or use StringToDouble().

But you need to be aware of the way that strings get typecasted into numbers i.e.

Forum on trading, automated trading systems and testing trading strategies

Issue with StringToDouble()

honest_knave, 2017.02.25 11:30

StringToDouble (and StrToDouble) will start from the left-most character.

  • It will skip whitespace at the start of a string, until a different character is encountered.
  • It will interpret one '+' or '-' as a positive or negative sign, but only when it is the first non-whitespace character encountered.
  • It will interpret number(s) in their literal sense.
  • It will interpret one '.' as a decimal point. 
  • As soon as it encounters whitespace after a non-whitespace character, or a 2nd decimal point, or a '+' or '-' sign that is not the first non-whitespace character, or any other character that is not a number it will disregard the rest of the string.
  • Note: it disregards the rest of the string, not the entire string.

Examples

StringDoubleReason 
"x12345"0.0First character was a letter
"123x45"123.0Encountered a letter after 123 
"    123"123.0Whitespace ignored at start of string 
"   12 3"12.0Encountered whitespace after 12
"123.45"123.45One decimal point is OK 
"123.4.5"123.4Encountered a second decimal point 
 "1,234.50"1.0Encountered a comma 

Not documented (to my knowledge)

If you're looking to extract certain numbers like sscanf does, that will definitely require your own function.

You may find some ideas in here or here. It will depend on how you expect your string to be formatted, and what you want to extract.

To add comments, please log in or register