Version française
Home     About     Download     Resources     Contact us    
Browse thread
index of substring
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Oliver Bandel <oliver@f...>
Subject: Re: [Caml-list] index of substring
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