Re: Reading in floats from strings???

"'Mr. Herr' [email protected] [ocaml_beginners]" <[email protected]> Wed, 27 Jan 2016 18:20:09 +0100
Newsgroups gmane.comp.lang.ocaml.beginners
Message-ID <[email protected]>

On 27.01.2016 16:29, Douglas Lewit [email protected] [ocaml_beginners] wrote:
>  
> Hi everyone,
>
> I am new to this list, so I thought I would introduce myself.  Also, I have a
> question about how to read in data of the form "11.3  12.9".  Obviously a string,
> but I need to extract the floats contained in the string. 

Hi,

this example in the ocaml toplevel might give you some ideas:

# #require "str";;
/opt/opam/4.02.3/lib/ocaml/str.cma: loaded
# let sample = "11.3  12.9";;
val sample : string = "11.3  12.9"
# let ls = Str.split (Str.regexp "[ ]+") sample;;
val ls : string list = ["11.3"; "12.9"]
# List.map float_of_string ls;;
- : float list = [11.3; 12.9]
# List.map float_of_string ("79,4 " :: ls);;(* show conversion error*)
Exception: Failure "float_of_string".
# List.map (fun s -> try float_of_string s with Failure _ -> 0.0) ("79,4 " :: ls);;
- : float list = [0.; 11.3; 12.9]
# (* show how to handle an exception *)

/Str.