I'm trying to get the floats from a string. I mean reading line by line from a text file and getting the floats from a line. I found how to read line by line but I couldn't split the string into floats. Here is an example input file:
10,10,18,18.1
7,3 ,10,14.2
3,3,5.3,5
I've looked at sscanf but I couldn't do it. Any idea?
From years of experience, I've found that scanf is unlikely to do exactly what you want. It's OK for a quick test program.
One possibility is to use Str.split
:
let floats_of_string s =
List.map float_of_string (Str.split (Str.regexp "[, \t]+") s)
You might need to make the regexp a little tighter if you want to detect invalid input.
Read the manual attentively to understand how scanf format works - it has some quirks but there is a reason behind them.
"%f , %f , %f , %f %!"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With