From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.2 required=5.0 tests=AWL,UNPARSEABLE_RELAY autolearn=disabled version=3.1.3 Received: from mail1-relais-roc.national.inria.fr (mail1-relais-roc.national.inria.fr [192.134.164.82]) by yquem.inria.fr (Postfix) with ESMTP id 01FF2BB84 for ; Fri, 29 Aug 2008 09:35:35 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AusCAEhDt0iTIFAHiGdsb2JhbACSSAEBAQ8gRKcogWo X-IronPort-AV: E=Sophos;i="4.32,291,1217800800"; d="scan'208";a="16583785" Received: from relay.felk.cvut.cz ([147.32.80.7]) by mail1-smtp-roc.national.inria.fr with ESMTP/TLS/DHE-RSA-AES256-SHA; 29 Aug 2008 09:35:34 +0200 X-FELK-MailScanner-Watermark: 1220600128.76919@9RDkYTPNZrrAvq8O12gOfA Received: from k333.felk.cvut.cz (k333.felk.cvut.cz [147.32.87.5]) by relay.felk.cvut.cz (8.14.2/8.14.2) with ESMTP id m7T7ZOaO061508 for ; Fri, 29 Aug 2008 09:35:24 +0200 (CEST) (envelope-from kybic@fel.cvut.cz) Received: from K333/SpoolDir by k333.felk.cvut.cz (Mercury 1.48); 29 Aug 08 09:35:24 +0100 Received: from SpoolDir by K333 (Mercury 1.48); 29 Aug 08 09:34:59 +0100 Received: from localhost (147.32.84.19) by k333.felk.cvut.cz (Mercury 1.48) with ESMTP; 29 Aug 08 09:34:57 +0100 To: caml-list@yquem.inria.fr Subject: Re: [Caml-list] Run-time evaluation of a Printf format string Reply-To: Jan Kybic From: Jan Kybic In-Reply-To: <48B6CBDC.9000702@glondu.net> (=?iso-8859-1?Q?St=E9phane?= Glondu's message of "Thu, 28 Aug 2008 18:01:32 +0200") References: <87hc951cr9.fsf@fel.cvut.cz> <48B6CBDC.9000702@glondu.net> Date: Fri, 29 Aug 2008 09:34:56 +0200 Message-ID: <87y72ge0kf.fsf@fel.cvut.cz> User-Agent: Gnus/5.1008 (Gnus v5.10.8) Emacs/22.2 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: quoted-printable X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.0 (relay.felk.cvut.cz [147.32.80.7]); Fri, 29 Aug 2008 09:35:24 +0200 (CEST) X-FELK-MailScanner-Information: X-MailScanner-ID: m7T7ZOaO061508 X-FELK-MailScanner: Found to be clean X-FELK-MailScanner-SpamCheck: not spam, SpamAssassin (not cached, score=-2.599, required 5, autolearn=not spam, BAYES_00 -2.60) X-FELK-MailScanner-From: kybic@fel.cvut.cz X-FELK-MailScanner-To: caml-list@yquem.inria.fr X-Spam: no; 0.00; run-time:01 printf:01 printf:01 sprintf:01 %03:01 ocaml:01 pervasives:01 scanf:01 syntax:01 foo:01 buffer:01 suitably:01 scanf:01 %03:01 sprintf:01 > Jan Kybic wrote: >> I would need an equivalent of Printf.sprintf where the=20 >> format string is not constant, it is read from the command line. >> The motivation is to let the user specify a template for file names, >> such as "img%03d.png". Can this be achieved in Ocaml? It seems not, as=20 >> Pervasives.string_of_format only accepts constant strings. >> Or is there some external library useful for this task? > > St=E9phane Glondu writes: > What about Scanf.format_from_string? > Dave Benjamin writes: > Does it have to be printf-style formatting? If you can switch to a > syntax like $(foo), you can use Buffer.add_substitute: Thank you for all your suggestions. Here is my summary: - If I only want to insert a suitably formatted sequence number, the simplest solution is using Scanf.format_from_string, such as let format =3D "img%03d.png" in=20 Printf.sprintf (Scanf.format_from_string format "%d") num - If I want to be able to insert several different variables (e.g. sequence number, image height and width) but do not have to specify formatting (like padding zeroes), then Buffer.add_substitute is easy enough to use. - If I want both to insert several different variables and to=20 specify the formatting, I will have to implement my own parser and interpreter of format strings. Jan --=20 ------------------------------------------------------------------------- Jan Kybic tel. +420 2 2435 5721 http://cmp.felk.cvut.cz/~kybic ICQ 200569450