Version française
Home     About     Download     Resources     Contact us    
Browse thread
printf and positional specifier
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Martin Jambon <martin1977@l...>
Subject: Re: [Caml-list] printf and positional specifier
On Sat, 11 Nov 2006, Peter Gregory wrote:

> Martin Jambon wrote:
>> On Sat, 11 Nov 2006, Peter Gregory wrote:
>> 
>>> Anastasia Gornostaeva wrote:
>>>> Hello.
>>>> 
>>>> $ ocaml
>>>>         Objective Caml version 3.09.3
>>>> 
>>>> # open Printf;;
>>>> # printf "%2$d %1$s" "abc" 2;;
>>>> Bad conversion %$, at char number 0 in format string ``%2$d %1$s''
>>>> 
>>>> 
>>>> How?
>>>> 
>>>> ermine
>>>> 
>>> 
>>> Hi Ermine,
>>> 
>>> I'm not sure that I understand your question.  I think to achieve what you 
>>> seem to be trying, you would simply write:
>>> 
>>> # printf "%d %s" 2 "abc";;
>>> 
>>> You just put the parameters in the order they came in the string.  Does 
>>> that help, it seems like perhaps you needed more than that.
>> 
>> The dollar stuff is real, although I have no idea of how to make it work. 
>> It's the last paragraph in the description of Printf.fprintf:
>>   http://caml.inria.fr/pub/docs/manual-ocaml/libref/Printf.html
>> 
>> 
>
> I take a different reading to you Martin, although I think it is confusing. 
> I believe that the number specified before the dollar sign is the argument 
> that specifies the /precision/ of the output.
> 
> So, it is not to specify the argument to print, but the argument to use as 
> precision.  That said, I tried it in the context they suggest and I couldn't 
> make it work!

Here is what I tried:

# open Printf;;
# let x = 1234.5678;;
val x : float = 1234.5678

(* Just for fun *)
# printf "%.2f %.*f" x 3 x;;
1234.57 1234.568- : unit = ()

(* I understand that the following should print "1234.57 1234.57": *)
# printf "%.2f %.*1$f" x x;;
Bad conversion %1, at char number 5 in format string ``%.2f %.*1$f''


Martin

--
Martin Jambon, PhD
http://martin.jambon.free.fr