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.0 required=5.0 tests=none autolearn=disabled version=3.1.3 X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr 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 D887ABB84 for ; Thu, 28 Aug 2008 17:02:11 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: ApoEAApbtkjOe3NG/2dsb2JhbAC6RoFq X-IronPort-AV: E=Sophos;i="4.32,286,1217800800"; d="scan'208";a="16562956" Received: from spoomusic.com ([206.123.115.70]) by mail1-smtp-roc.national.inria.fr with ESMTP/TLS/DHE-RSA-AES256-SHA; 28 Aug 2008 17:01:54 +0200 Received: (qmail 11290 invoked by uid 89); 28 Aug 2008 15:01:52 -0000 Received: from unknown (HELO ?192.168.1.99?) (98.165.129.233) by 0 with ESMTPS (DHE-RSA-AES256-SHA encrypted); 28 Aug 2008 15:01:52 -0000 Message-ID: <48B6BDD3.7020704@ramenlabs.com> Date: Thu, 28 Aug 2008 08:01:39 -0700 From: Dave Benjamin User-Agent: Mozilla-Thunderbird 2.0.0.14 (X11/20080509) MIME-Version: 1.0 To: Jan Kybic Cc: caml-list@yquem.inria.fr Subject: Re: [Caml-list] Run-time evaluation of a Printf format string References: <87hc951cr9.fsf@fel.cvut.cz> In-Reply-To: <87hc951cr9.fsf@fel.cvut.cz> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Spam: no; 0.00; run-time:01 printf:01 printf:01 sprintf:01 %03:01 ocaml:01 pervasives:01 syntax:01 foo:01 buffer:01 buffer:01 val:01 abstr:01 vars:01 val:01 Jan Kybic wrote: > I would need an equivalent of Printf.sprintf where the > 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 > Pervasives.string_of_format only accepts constant strings. > Or is there some external library useful for this task? Does it have to be printf-style formatting? If you can switch to a syntax like $(foo), you can use Buffer.add_substitute: # Buffer.add_substitute;; - : Buffer.t -> (string -> string) -> string -> unit = # let buffer = Buffer.create 0;; val buffer : Buffer.t = # let vars = ["who", "world"];; val vars : (string * string) list = [("who", "world")] # Buffer.add_substitute buffer (fun name -> List.assoc name vars) "hello $(who)";; - : unit = () # Buffer.contents buffer;; - : string = "hello world" Dave