Browse thread
Run-time evaluation of a Printf format string
-
Jan Kybic
- David Teller
- Edgar Friendly
- Dave Benjamin
- Stéphane Glondu
[
Home
]
[ Index:
by date
|
by threads
]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
| Date: | -- (:) |
| From: | Dave Benjamin <dave@r...> |
| Subject: | Re: [Caml-list] Run-time evaluation of a Printf format string |
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 = <fun>
# let buffer = Buffer.create 0;;
val buffer : Buffer.t = <abstr>
# 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