> Can this Stream reading make use of the scanf to read floats (and other words)?

Not really (although you can make do with Scanf.Scanning.from_function : (unit -> char) -> Scanning.in_channel; https://caml.inria.fr/pub/docs/manual-ocaml/libref/Scanf.Scanning.html ).

If counting the line number is important to you, it makes sense to keep using input_line, instead of scanning " %f" directly on the channel (as this may skip arbitrarily many newlines) but then you can still use it to scan each line as a string:

  let line = input_line channel in
  let scanbuf = Scanf.Scanning.from_string line in
  incr line_number;
  Scanf.bscanf "%s@ " ignore;
  let vec = Array.init !dims (fun _ -> Scanf.bscanf scanbuf " %f" (fun x -> x)) in
  ...

(the format "%s@c" means "scan a string until the character (c) excluded, so "%s@ " consumes the first word.)

On Wed, Apr 26, 2017 at 10:05 AM, Jon Kleiser <jon.kleiser@ceres.no> wrote:
Thanks a lot, Gabriel, for your idea about using the “word by word” method. This far I have used the Stream way of file reading:

let line_stream_of_channel channel =
  Stream.from
    (fun _ -> try Some (input_line channel) with End_of_file -> None)

Can this Stream reading make use of the scanf to read floats (and other words)? If not, I may leave the Stream way.

I would also like to have access to the current number of lines received, to be able to report that so-and-so was found at line number x. This far I have not found out how count the lines while reading from a Stream.

/Jon


> On 26. Apr, 2017, at 15:41, Gabriel Scherer <gabriel.scherer@gmail.com> wrote:
>
> It looks like you read a line from an input channel and now want to split it on its spaces. It is also possible to read the input channel word by word in the first place, and for this the semantics of spaces in a scanf format is very useful: a single space ignores all whitespace. So
>
> let read_float () =
>   Scanf.scanf " %f" (fun x -> x)
>
> will ignore any whitespace and then expect a floating-point number, read it and return it. (This reads from standard input, to read from arbitrary channels see Scanf.bscanf and the Scanf.Scanning module).
>
> On Wed, Apr 26, 2017 at 6:48 AM, Jon Kleiser <jon.kleiser@ceres.no> wrote:
> Hi,
>
> I am quite new to OCaml, and I am looking for the most efficient way to make an Array of floats from string. My solution this far looks like this, where dims is a global variable specifying the size of the Arrays (typically 300):
>
> let make_vector vec_strings =
>   let vec = Array.make !dims 0.0 in
>   List.iteri (fun i str -> vec.(i) <- float_of_string str) vec_strings
>
> let process_line line =
>   let parts = Str.split (Str.regexp " ") line in
>   make_vector (List.tl parts)   (* skipping first element which is not a float *)
>
> /Jon
>
> --
> Caml-list mailing list.  Subscription management and archives:
> https://sympa.inria.fr/sympa/arc/caml-list
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
>


--
Caml-list mailing list.  Subscription management and archives:
https://sympa.inria.fr/sympa/arc/caml-list
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs