From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@sympa.inria.fr Delivered-To: caml-list@sympa.inria.fr Received: from mail3-relais-sop.national.inria.fr (mail3-relais-sop.national.inria.fr [192.134.164.104]) by sympa.inria.fr (Postfix) with ESMTPS id 196F87FA5E for ; Wed, 26 Apr 2017 17:26:51 +0200 (CEST) Authentication-Results: mail3-smtp-sop.national.inria.fr; spf=None smtp.pra=gabriel.scherer@gmail.com; spf=Pass smtp.mailfrom=gabriel.scherer@gmail.com; spf=None smtp.helo=postmaster@mail-qk0-f173.google.com Received-SPF: None (mail3-smtp-sop.national.inria.fr: no sender authenticity information available from domain of gabriel.scherer@gmail.com) identity=pra; client-ip=209.85.220.173; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="gabriel.scherer@gmail.com"; x-sender="gabriel.scherer@gmail.com"; x-conformance=sidf_compatible Received-SPF: Pass (mail3-smtp-sop.national.inria.fr: domain of gabriel.scherer@gmail.com designates 209.85.220.173 as permitted sender) identity=mailfrom; client-ip=209.85.220.173; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="gabriel.scherer@gmail.com"; x-sender="gabriel.scherer@gmail.com"; x-conformance=sidf_compatible; x-record-type="v=spf1" Received-SPF: None (mail3-smtp-sop.national.inria.fr: no sender authenticity information available from domain of postmaster@mail-qk0-f173.google.com) identity=helo; client-ip=209.85.220.173; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="gabriel.scherer@gmail.com"; x-sender="postmaster@mail-qk0-f173.google.com"; x-conformance=sidf_compatible IronPort-PHdr: =?us-ascii?q?9a23=3AWMTNlRcCd0Yc2Et6Hn8oCe3vlGMj4u6mDksu8pMi?= =?us-ascii?q?zoh2WeGdxcSyYB7h7PlgxGXEQZ/co6odzbGH7+a4ASQp2tWoiDg6aptCVhsI24?= =?us-ascii?q?09vjcLJ4q7M3D9N+PgdCcgHc5PBxdP9nC/NlVJSo6lPwWB6nK94iQPFRrhKAF7?= =?us-ascii?q?Ovr6GpLIj8Swyuu+54Dfbx9GiTe5br5+Ngm6oRnMvcQKnIVuLbo8xAHUqXVSYe?= =?us-ascii?q?RWwm1oJVOXnxni48q74YBu/SdNtf8/7sBMSar1cbg2QrxeFzQmLns65Nb3uhnZ?= =?us-ascii?q?TAuA/WUTX2MLmRdVGQfF7RX6XpDssivms+d2xSeXMdHqQb0yRD+v9LlgRgP2hy?= =?us-ascii?q?gbNj456GDXhdJ2jKJHuxKquhhzz5fJbI2JKPZye6XQctQHS2pcRcZRTzJODZ+g?= =?us-ascii?q?b4UBCOoBOPxXr4j7p1ATqRezCg2hCObpxzRVhHH5wLc63vwjHgHI3AIuEdEAvm?= =?us-ascii?q?nKotrpL6odS/y5wbPSwDnfc/9b2zHw45XIfBA7pvGMWKp9f8vLxkY0DQPFj0uf?= =?us-ascii?q?qYj4ND2IyusNs3aU7/B+WuK1lmUqrA5xrSK0ycc2i4nGmIYVxkrf9SplwIY6O8?= =?us-ascii?q?e4R1BhYdO/H5tQsjuVN4pyQs84X25ovyM6x6QAtJWmciYKz5EnyATea/yBa4WI?= =?us-ascii?q?4xTjVPyQIThinn5ldqi/iw6y8Ee6yu38Uc603ExWoSpCl9nBsG0G2R/L6sWfVP?= =?us-ascii?q?dx4kOs1SyM2g3T8O1IP144mbbBJ5I8wLM9mJweulnZECDsgkX5lqqWe10k+ue2?= =?us-ascii?q?7+TnZa3rppqGOI91jgHyK70ums+jDeghPAgCQmqW9Oqm2L3s+k35R7pKjvkona?= =?us-ascii?q?XDrJ/aIsEbqra4Aw9TzIkj9w6yAym63Nkch3UKL1JIdAiZg4T3J13COu30APWx?= =?us-ascii?q?jli0lTdk3fHGPrnvApXXKXjDla/sfbln5EFH0gozzM5Q55ZOBrEaPP3zWlH+tM?= =?us-ascii?q?bGAR8jKAG0zOPnB8981oMaQ26AHqiZMKbKvV+S+u0vO/WMZJMSuDvlN/cq/fvu?= =?us-ascii?q?jXsglV8ZfKmpxocXZWujHvVmJkWZeWDjjs0AEWcMpAo+TfblhEeMUT5JND6OWP?= =?us-ascii?q?ce4TdzK4arDJzOXMj5gruHmiS/E5xNa3puAV2WHGyucYLSCNkWbyfHDcZriD0J?= =?us-ascii?q?UfCaQI8szxy0/Fv1wrB9L+fQvDYTtZ/52cJd6OjalBV0/jtxWZfOm1qRRn15yz?= =?us-ascii?q?tbDwQ927py9Akgk1o=3D?= X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: =?us-ascii?q?A0ChAgCHuwBZf63cVdFbGgEBAQECAQEBA?= =?us-ascii?q?QgBAQEBFwEBBAEBCgEBhAuBDAeDYZtggj2TLoIPLIV4AoQdB0AXAQEBAQEBAQE?= =?us-ascii?q?BAQESAQEJCwsIJjGCMyKCQAEBAQECASMEGQEbEgsBAwELBgULDQ0dAgIhAQERA?= =?us-ascii?q?QUBChIGExIJiWcBAw0IDp1xP4wIgWwYBQEcgwoFg2UKGScDClaDCQEBAQEBAQQ?= =?us-ascii?q?BAQEBAQEBARgCBhKGQoR4glOCOYJZgl8Fh18MlSg7ghCFCYcnhEuCAlWIR4ZAi?= =?us-ascii?q?xmHRhQfgRUPEgGBPCcdIVkZhBsqgiokNQEBiHMBAQE?= X-IPAS-Result: =?us-ascii?q?A0ChAgCHuwBZf63cVdFbGgEBAQECAQEBAQgBAQEBFwEBBAE?= =?us-ascii?q?BCgEBhAuBDAeDYZtggj2TLoIPLIV4AoQdB0AXAQEBAQEBAQEBAQESAQEJCwsIJ?= =?us-ascii?q?jGCMyKCQAEBAQECASMEGQEbEgsBAwELBgULDQ0dAgIhAQERAQUBChIGExIJiWc?= =?us-ascii?q?BAw0IDp1xP4wIgWwYBQEcgwoFg2UKGScDClaDCQEBAQEBAQQBAQEBAQEBARgCB?= =?us-ascii?q?hKGQoR4glOCOYJZgl8Fh18MlSg7ghCFCYcnhEuCAlWIR4ZAixmHRhQfgRUPEgG?= =?us-ascii?q?BPCcdIVkZhBsqgiokNQEBiHMBAQE?= X-IronPort-AV: E=Sophos;i="5.37,255,1488841200"; d="scan'208,217";a="221620171" Received: from mail-qk0-f173.google.com ([209.85.220.173]) by mail3-smtp-sop.national.inria.fr with ESMTP/TLS/AES128-GCM-SHA256; 26 Apr 2017 17:26:49 +0200 Received: by mail-qk0-f173.google.com with SMTP id h123so3773973qke.0 for ; Wed, 26 Apr 2017 08:26:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=qUVHMJukW4Ne4BuHLG9WKgO92DqZrTwi+ICEwQnp4GE=; b=UHQ6hX15fgKNNIlM/wxhj95Y5mskKSnzBjiXR7PIKpyJta90m/cmw8Hmk6x3+bCPH+ afaMB3Gpf9PMYYOPai4tXseT9J2fkg+nrXuqb9Zzl7LazpN5Ahd6c/ivy6nIxOszDAnl LBJkkhRpFIyr/GwNbcYGsOrMXFo7CqIU0TKGku23PCxnSAy82nybvaJY7TrytX8exrTp krxgAlr9s30tAeDBCFPcO9ADO9ro14HBnTwxulNk/Ag8G/foHjKApU+tkrF1ncY4OuE8 bZ8Wa+5XVaKtzVBhIlPNVQzvO7bg+DlGR/sk0qhhDpqlomBqqIR3lL3/d7GCzbGD7jnX ycYQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=qUVHMJukW4Ne4BuHLG9WKgO92DqZrTwi+ICEwQnp4GE=; b=ijC+Tcwudxfz8xifbaiR1q2c643qbvM6GzgDhFPAkObLirj7dW7SGJ7pCe3oUmodLy jlxINEJDITXZI8cidNuSPIr6vhPLY65uxypEmXVIfeDFS/PRtIyHXUZorjdNREhUbsvy bsIPyHrHvzZ0/sTLZWHFpOAlTn8Gv8HLCG+I6V/LMyo4AKbyjwT+DR1hR7qmWRpgyCyC ZcybB6AZe5qeILHnC0yF6Nzu6ZGWKP04pRf9jfIJka9p8mWyapsdicqDTXrz8U5+/BiY qApOIQNkJmw449L0ceGnPWI9vItdVS4rU/j7lfAt0S2JxZ5+/U7t6qba+FE/6G3bITFn blPA== X-Gm-Message-State: AN3rC/7LTquUcYHJSiwdp3ADUHenImwUU6algrJfeckZTk3FwbxGc7LV wx2wQWanphWzi5CzJgbYZzzL2dIirg== X-Received: by 10.55.146.135 with SMTP id u129mr345107qkd.124.1493220408549; Wed, 26 Apr 2017 08:26:48 -0700 (PDT) MIME-Version: 1.0 Received: by 10.12.183.2 with HTTP; Wed, 26 Apr 2017 08:26:07 -0700 (PDT) In-Reply-To: References: <2C817F94-9BF5-451F-BB12-706E441312D0@mail.uio.no> From: Gabriel Scherer Date: Wed, 26 Apr 2017 11:26:07 -0400 Message-ID: To: Jon Kleiser Cc: "caml-list@inria.fr" Content-Type: multipart/alternative; boundary=94eb2c08b92405ebe0054e1378a6 Subject: Re: [Caml-list] Create Array of floats from string --94eb2c08b92405ebe0054e1378a6 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable > 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 =3D input_line channel in let scanbuf =3D Scanf.Scanning.from_string line in incr line_number; Scanf.bscanf "%s@ " ignore; let vec =3D 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 wrote: > Thanks a lot, Gabriel, for your idea about using the =E2=80=9Cword by wor= d=E2=80=9D > method. This far I have used the Stream way of file reading: > > let line_stream_of_channel channel =3D > 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 > 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 sca= nf > format is very useful: a single space ignores all whitespace. So > > > > let read_float () =3D > > 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 > 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 =3D > > let vec =3D Array.make !dims 0.0 in > > List.iteri (fun i str -> vec.(i) <- float_of_string str) vec_strings > > > > let process_line line =3D > > let parts =3D 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 > --94eb2c08b92405ebe0054e1378a6 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
> Can this Stream reading make= use of the scanf to read floats (and other words)?

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

If counting the line num= ber is important to you, it makes sense to keep using input_line, instead o= f scanning " %f" directly on the channel (as this may skip arbitr= arily many newlines) but then you can still use it to scan each line as a s= tring:

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

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

On Wed, Apr 26, 2= 017 at 10:05 AM, Jon Kleiser <jon.kleiser@ceres.no> wrote= :
Thanks a lot, Gabriel, for your idea ab= out using the =E2=80=9Cword by word=E2=80=9D method. This far I have used t= he Stream way of file reading:

let line_stream_of_channel channel =3D
=C2=A0 Stream.from
=C2=A0 =C2=A0 (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 wor= ds)? If not, I may leave the Stream way.

I would also like to have access to the current number of lines received, t= o be able to report that so-and-so was found at line number x. This far I h= ave 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 sp= lit 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 f= ormat is very useful: a single space ignores all whitespace. So
>
> let read_float () =3D
>=C2=A0 =C2=A0Scanf.scanf " %f" (fun x -> x)
>
> will ignore any whitespace and then expect a floating-point number, re= ad it and return it. (This reads from standard input, to read from arbitrar= y 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 t= o 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 (typica= lly 300):
>
> let make_vector vec_strings =3D
>=C2=A0 =C2=A0let vec =3D Array.make !dims 0.0 in
>=C2=A0 =C2=A0List.iteri (fun i str -> vec.(i) <- float_of_string = str) vec_strings
>
> let process_line line =3D
>=C2=A0 =C2=A0let parts =3D Str.split (Str.regexp " ") line in=
>=C2=A0 =C2=A0make_vector (List.tl parts)=C2=A0 =C2=A0(* skipping first = element which is not a float *)
>
> /Jon
>
> --
> Caml-list mailing list.=C2=A0 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.=C2=A0 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

--94eb2c08b92405ebe0054e1378a6--