caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* printf & scanf oddity
@ 2006-01-27 14:26 Basile STARYNKEVITCH
  2006-01-27 14:57 ` [Caml-list] " Jean-Christophe Filliatre
  2006-01-27 15:03 ` Virgile Prevosto
  0 siblings, 2 replies; 4+ messages in thread
From: Basile STARYNKEVITCH @ 2006-01-27 14:26 UTC (permalink / raw)
  To: caml-list

Dear All

I find the following quite strange:

% ocaml
        Objective Caml version 3.09.1

# Printf.sprintf "_a%d_f%d" 1 2;;
- : string = "_a1_f2"
# Scanf.sscanf "_a1_f2" "_a%d_f%d" (fun x y -> x,y);;
Exception:
Scanf.Scan_failure
 "scanf: bad input at char number 5: looking for '_', found 'f'".


Is it a bug, or did I misunderstood something?

Regards.
-- 
Basile STARYNKEVITCH         http://starynkevitch.net/Basile/ 
email: basile<at>starynkevitch<dot>net 
aliases: basile<at>tunes<dot>org = bstarynk<at>nerim<dot>net
8, rue de la Faïencerie, 92340 Bourg La Reine, France


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Caml-list] printf & scanf oddity
  2006-01-27 14:26 printf & scanf oddity Basile STARYNKEVITCH
@ 2006-01-27 14:57 ` Jean-Christophe Filliatre
  2006-01-27 15:48   ` William D. Neumann
  2006-01-27 15:03 ` Virgile Prevosto
  1 sibling, 1 reply; 4+ messages in thread
From: Jean-Christophe Filliatre @ 2006-01-27 14:57 UTC (permalink / raw)
  To: Basile STARYNKEVITCH; +Cc: caml-list


Basile STARYNKEVITCH writes:
 > 
 > I find the following quite strange:
 > 
 > % ocaml
 >         Objective Caml version 3.09.1
 > 
 > # Printf.sprintf "_a%d_f%d" 1 2;;
 > - : string = "_a1_f2"
 > # Scanf.sscanf "_a1_f2" "_a%d_f%d" (fun x y -> x,y);;
 > Exception:
 > Scanf.Scan_failure
 >  "scanf: bad input at char number 5: looking for '_', found 'f'".
 > 
 > Is it a bug, or did I misunderstood something?

I think this is the answer :-)

======================================================================
        Objective Caml version 3.08.3

# 1_;;
- : int = 1
# 1_2_3;;
- : int = 123
etc.
======================================================================

See "integer literals" in the manual : 
http://caml.inria.fr/pub/docs/manual-ocaml/manual009.html

But I agree with you: this is wierd and almost a bug...

-- 
Jean-Christophe Filliâtre (http://www.lri.fr/~filliatr)


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Caml-list] printf & scanf oddity
  2006-01-27 14:26 printf & scanf oddity Basile STARYNKEVITCH
  2006-01-27 14:57 ` [Caml-list] " Jean-Christophe Filliatre
@ 2006-01-27 15:03 ` Virgile Prevosto
  1 sibling, 0 replies; 4+ messages in thread
From: Virgile Prevosto @ 2006-01-27 15:03 UTC (permalink / raw)
  To: caml-list

Hello,
Le ven 27 jan 2006 15:26:22 CET, Basile STARYNKEVITCH a écrit:
> % ocaml
>         Objective Caml version 3.09.1
> 
> # Printf.sprintf "_a%d_f%d" 1 2;;
> - : string = "_a1_f2"
> # Scanf.sscanf "_a1_f2" "_a%d_f%d" (fun x y -> x,y);;
> Exception:
> Scanf.Scan_failure
>  "scanf: bad input at char number 5: looking for '_', found 'f'".
> 

I'd say that it comes from the fact that in ocaml syntax '_' can be used
to separate blocks of numerals in integer and float literals:
the first integer read by sscanf is "1_" and not "1". There might be a
workaround by using %[0-9] instead of %d (and int_of_string in the final
computation of course).


-- 
E tutto per oggi, a la prossima volta
Virgile


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Caml-list] printf & scanf oddity
  2006-01-27 14:57 ` [Caml-list] " Jean-Christophe Filliatre
@ 2006-01-27 15:48   ` William D. Neumann
  0 siblings, 0 replies; 4+ messages in thread
From: William D. Neumann @ 2006-01-27 15:48 UTC (permalink / raw)
  To: Jean-Christophe Filliatre; +Cc: Basile STARYNKEVITCH, caml-list

On Fri, 27 Jan 2006, Jean-Christophe Filliatre wrote:

> See "integer literals" in the manual :
> http://caml.inria.fr/pub/docs/manual-ocaml/manual009.html
>
> But I agree with you: this is wierd and almost a bug...

Even more clearly, see the Scanf module documentation.  From page 349 of 
the 3.09 manual:

*in addition to relevant digits, _ characters may appear inside numbers 
(this is reminiscent to the usual Caml conventions). If stricter scanning 
is desired, use the range conversion facility instead of the number 
conversions.

So, for example, this works:
# Scanf.sscanf "_a14_f2" "_a%[0-9]_f%[0-9]" (fun x y -> x,y);;
- : string * string = ("14", "2")

Though the return type is string, not int, so you'd need to convert them 
if you want ints...

William D. Neumann

---

"There's just so many extra children, we could just feed the
children to these tigers.  We don't need them, we're not doing 
anything with them.

Tigers are noble and sleek; children are loud and messy."

         -- Neko Case

Life is unfair.  Kill yourself or get over it.
 	-- Black Box Recorder


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2006-01-27 15:47 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-01-27 14:26 printf & scanf oddity Basile STARYNKEVITCH
2006-01-27 14:57 ` [Caml-list] " Jean-Christophe Filliatre
2006-01-27 15:48   ` William D. Neumann
2006-01-27 15:03 ` Virgile Prevosto

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).