Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] How to read three integers from a text-file... ?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Jacques Garrigue <garrigue@k...>
Subject: Re: [Caml-list] How to read three integers from a text-file... ?
From: Jacek Chrzaszcz <chrzaszcz@mimuw.edu.pl>

> Is there a clean way (a one-liner) to read a constant number of
> integers separated by whitespace from a text-file (or stdin) ?
> 
> I mean something like 
> scanf("%d%d%d",&a,&b,&c) in C, or
> readln(a,b,c) in Pascal

A clean way to do this is to use then Genlex module.

# #load"camlp4o.cma";;
# open Genlex;;
# let s = Genlex.make_lexer [] (Stream.of_channel stdin);;
val s : Genlex.token Stream.t = <abstr>
# match s with parser [< 'Int x; 'Int y; 'Int z >] -> (x, y, z);;
3 5 7
- : int * int * int = 3, 5, 7

If you don't want to use camlp4o.cma, it is possible with Stream.next,
but a bit more painful.

Cheers,

Jacques Garrigue
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners