From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Delivered-To: caml-list@yquem.inria.fr Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by yquem.inria.fr (Postfix) with ESMTP id 2F073BB91 for ; Thu, 27 Jan 2005 09:54:33 +0100 (CET) Received: from first.in-berlin.de (dialin-145-254-063-011.arcor-ip.net [145.254.63.11]) by concorde.inria.fr (8.13.0/8.13.0) with ESMTP id j0R8sVnt018178 for ; Thu, 27 Jan 2005 09:54:32 +0100 Received: by first.in-berlin.de (Postfix, from userid 501) id 02B6FA4746; Thu, 27 Jan 2005 09:37:49 +0100 (CET) Date: Thu, 27 Jan 2005 09:37:49 +0100 From: Oliver Bandel To: caml-list@yquem.inria.fr Subject: Re: [Caml-list] index of substring Message-ID: <20050127083749.GA336@first.in-berlin.de> References: <1106786478.41f838aecb7dd@www.doc.ic.ac.uk> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <1106786478.41f838aecb7dd@www.doc.ic.ac.uk> User-Agent: Mutt/1.5.6i X-Miltered: at concorde with ID 41F8AC47.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; oliver:01 bandel:01 oliver:01 in-berlin:01 caml-list:01 wrote:01 ocamllex:01 ocamllex:01 substrings:01 regexp:01 regexp:01 fstat:01 char:01 ocaml:01 pervasives:01 X-Spam-Checker-Version: SpamAssassin 3.0.0 (2004-09-13) on yquem.inria.fr X-Spam-Status: No, score=0.1 required=5.0 tests=FORGED_RCVD_HELO autolearn=disabled version=3.0.0 X-Spam-Level: On Thu, Jan 27, 2005 at 12:41:18AM +0000, yjc01@doc.ic.ac.uk wrote: > I tried to write a function to return the index of a substring in a string from > an index. What? Can you explain your problem with an example? What do you want to achieve? Example of input/output-data...?! > So I can use it to extract some comments (text between (* and *) in a > file. But came across some problem when returning the value. Well, if you want to return the comments, you may use ocamllex. There is a tutorial on ocamllex, which some weeks ago was announced here. I have read it and it should explain enough and detailed so that you may use ocamllex. But BTW: There is a Str-module. You can get index-values for matched substrings with it. You have to use a regular expression (regexp) to match the strings. You first have to compile the regexp with Str.regexp. After you have compiled the regexp-string (definition) into a regexp-type with Str.regexp, you can use that compiled regexp with the matching functions of the Str-module. > > The code I came up with is: > (* Use Unix Module *) > open Unix;; > > > (* get the file size *) > let fileReader = openfile "student.cd" [O_RDONLY] 0o640;; > let fileSize = (fstat fileReader).st_size;; > print_int fileSize;; > > (* create variable to store file *) > let comments = String.make fileSize 'c';; > > > let read_char () = > read fileReader comments 0 fileSize;; let filename = "student.cd" let inchannel = open_in filename You can read such channels with many functions. They are all documented in the OCaml reference manual. Look in chapter 19 of the Manual (Core Library) and look for "Pervasives". For example you can read one line from that channel with the function input_line. So with the definitions above, you read a line as follows: let next_line () = input_line inchannel After you are ready with that file (you get an exception which indicates end of file), you can close it with the close_in function. Ciao, Oliver