From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Delivered-To: caml-list@yquem.inria.fr Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by yquem.inria.fr (Postfix) with ESMTP id 4A973BB91 for ; Thu, 27 Jan 2005 01:41:20 +0100 (CET) Received: from pauillac.inria.fr (pauillac.inria.fr [128.93.11.35]) by nez-perce.inria.fr (8.13.0/8.13.0) with ESMTP id j0R0fJue013159 for ; Thu, 27 Jan 2005 01:41:20 +0100 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 BAA31449 for ; Thu, 27 Jan 2005 01:41:19 +0100 (MET) Received: from roadrunner.doc.ic.ac.uk (roadrunner.doc.ic.ac.uk [146.169.1.193]) by nez-perce.inria.fr (8.13.0/8.13.0) with ESMTP id j0R0fJs6013156 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO) for ; Thu, 27 Jan 2005 01:41:19 +0100 Received: from linnet.doc.ic.ac.uk ([146.169.1.10]) by roadrunner.doc.ic.ac.uk with esmtps (TLSv1:AES256-SHA:256) (Exim 4.43) id 1Ctxio-0005ZV-U3 for caml-list@inria.fr; Thu, 27 Jan 2005 00:41:18 +0000 Received: from hordewebuser by linnet.doc.ic.ac.uk with local (Exim 4.20) id 1Ctxio-0002gp-Rq for caml-list@inria.fr; Thu, 27 Jan 2005 00:41:18 +0000 Received: from 82-44-200-78.cable.ubr08.haye.blueyonder.co.uk (82-44-200-78.cable.ubr08.haye.blueyonder.co.uk [82.44.200.78]) by www.doc.ic.ac.uk (IMP) with HTTP for ; Thu, 27 Jan 2005 00:41:18 +0000 Message-ID: <1106786478.41f838aecb7dd@www.doc.ic.ac.uk> Date: Thu, 27 Jan 2005 00:41:18 +0000 From: yjc01@doc.ic.ac.uk To: caml-list@inria.fr Subject: index of substring MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit User-Agent: Internet Messaging Program (IMP) 3.2 X-Originating-IP: 82.44.200.78 Sender: hordewebuser X-Miltered: at nez-perce with ID 41F838AF.001 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Miltered: at nez-perce with ID 41F838AF.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; fstat:01 char:01 daisy:98 unix:01 unix:01 matched:01 int:01 int:01 string:03 string:03 module:03 let:03 let:03 character:03 size:95 X-Spam-Checker-Version: SpamAssassin 3.0.0 (2004-09-13) on yquem.inria.fr X-Spam-Status: No, score=0.7 required=5.0 tests=FROM_ENDS_IN_NUMS, NO_REAL_NAME autolearn=disabled version=3.0.0 X-Spam-Level: I tried to write a function to return the index of a substring in a string from an index. So I can use it to extract some comments (text between (* and *) in a file. But came across some problem when returning the value. 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;; (* return index of a substring in a string from an index, if substring not matched retun -1 *) let index_left fromIndex string findString = (* Step 1. Find first character *) let firstIndex = String.index_from string fromIndex (String.get findString 0) in if (firstIndex >= 0) then let isMatched = ref true in for i = 1 to (String.length findString) do if (String.get string firstIndex) != (String.get findString 0) then !isMatched = false; if (!isMatched) then firstIndex else -1; done;; (* test if index_left works *) let check index = index_left index comments "(*" print_int check 1;; Or is there an easier way to do this? Daisy