From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id QAA25672; Mon, 2 Feb 2004 16:54:44 +0100 (MET) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id QAA25990 for ; Mon, 2 Feb 2004 16:54:44 +0100 (MET) Received: from pop19.ucdavis.edu (pop19.ucdavis.edu [169.237.105.29]) by nez-perce.inria.fr (8.11.1/8.11.1) with ESMTP id i12Fsgv10131 for ; Mon, 2 Feb 2004 16:54:43 +0100 (MET) Received: from manzanita ([128.120.141.214]) by pop19.ucdavis.edu (8.12.10/8.12.9/it-std-5.2.0) with SMTP id i12FsfoV014820 for ; Mon, 2 Feb 2004 07:54:41 -0800 (PST) Received: by manzanita (sSMTP sendmail emulation); Mon, 2 Feb 2004 07:54:06 -0800 From: "Issac Trotts" Date: Mon, 2 Feb 2004 07:54:06 -0800 To: caml-list@inria.fr Subject: Re: [Caml-list] laconical input from a file for arrays and/or matrices Message-ID: <20040202155406.GA5103@manzanita> Mail-Followup-To: ijtrotts@ucdavis.edu, caml-list@inria.fr References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.4i X-Loop: caml-list@inria.fr X-Spam: no; 0.00; issac:01 trotts:01 ijtrotts:01 caml-list:01 2004:99 fragment:01 iostream:01 cin:99 cin:99 resize:01 resize:01 fragment:01 myprogram:01 camlp:01 camlp:01 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk On Mon, Feb 02, 2004 at 11:42:18AM +0100, Khamenia, Valery wrote: > Hi OCaMLers, > > here goes my usual way to input ASCII matrices from file in C++: > > // ------- start of fragment ----------- > #include > #include > using namespace std; > > void input_matrix(vector >&vec) { > int m, n; > cin >> m; > cin >> n; > vec.resize (m); > for( int i = 0; i vec[i].resize(n); > for( int i = 0; i for( int j = 0; j cin >> vec[i][j]; > } > // -------- end of fragment ------------- > > then for matrix : > > 3 > 3 > 1 2 3 > 4 5 6 > 7 8 9 > > stored in ASCII text file mymatrix.dat > > i just run my program: > > % myprogram < mymatrix.dat > > and it is done. > > Thus, the few lines of code and i can apply all math I need. Here's a way to do it with camlp4. You get OCaml-style comments for free from Genlex this way: #load "camlp4o.cma";; open Genlex;; let input_matrix channel = let read_number = parser [< 'Int i >] -> float i | [< 'Float f >] -> f in let rec read_nums n = if n = 0 then parser [< >] -> [] else parser [< head=read_number; tail=read_nums (n-1) >] -> head::tail in let rec read_matrix_data m n = if m = 0 then parser [< >] -> [] else parser [< row = read_nums n; rest = read_matrix_data (m-1) n >] -> (Array.of_list row) :: rest in let read_matrix = parser [< 'Int m; 'Int n; rest >] -> let rows = read_matrix_data m n rest in Array.of_list rows in let charstream = Stream.of_channel channel in let lexer = Genlex.make_lexer [] charstream in read_matrix lexer;; input_matrix stdin;; -- Issac Trotts http://redwood.ucdavis.edu/~issac ------------------- 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