Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Jan Kybic <kybic@fel.cvut.cz>
To: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] Run-time evaluation of a Printf format string
Date: Fri, 29 Aug 2008 09:34:56 +0200	[thread overview]
Message-ID: <87y72ge0kf.fsf@fel.cvut.cz> (raw)
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")

> 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?
>

> Stéphane Glondu <steph@glondu.net> writes:
> What about Scanf.format_from_string?

> Dave Benjamin <dave@ramenlabs.com> 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 = "img%03d.png" in 
  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 
  specify the formatting, I will have to implement my own parser and
  interpreter of format strings.

Jan



-- 
-------------------------------------------------------------------------
Jan Kybic <kybic@fel.cvut.cz>                       tel. +420 2 2435 5721
http://cmp.felk.cvut.cz/~kybic                      ICQ 200569450


      reply	other threads:[~2008-08-29  7:35 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-08-28 13:37 Jan Kybic
2008-08-28 14:10 ` [Caml-list] " David Teller
2008-08-28 14:58 ` Edgar Friendly
2008-08-28 15:01 ` Dave Benjamin
2008-08-28 16:01 ` Stéphane Glondu
2008-08-29  7:34   ` Jan Kybic [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87y72ge0kf.fsf@fel.cvut.cz \
    --to=kybic@fel.cvut.cz \
    --cc=caml-list@yquem.inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox