From: Oliver Bandel <oliver@first.in-berlin.de>
To: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] index of substring
Date: Thu, 27 Jan 2005 09:37:49 +0100 [thread overview]
Message-ID: <20050127083749.GA336@first.in-berlin.de> (raw)
In-Reply-To: <1106786478.41f838aecb7dd@www.doc.ic.ac.uk>
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
next prev parent reply other threads:[~2005-01-27 8:54 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2005-01-27 0:41 yjc01
2005-01-27 4:21 ` [Caml-list] " Matt Gushee
2005-01-27 6:44 ` Radu Grigore
2005-01-27 9:36 ` skaller
2005-01-27 10:20 ` Jean-Christophe Filliatre
2005-01-27 16:55 ` skaller
2005-01-27 17:20 ` Radu Grigore
2005-01-28 8:51 ` Jean-Christophe Filliatre
2005-01-28 13:31 ` skaller
2005-01-27 8:37 ` Oliver Bandel [this message]
2005-01-27 9:17 ` Xavier Leroy
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20050127083749.GA336@first.in-berlin.de \
--to=oliver@first.in-berlin.de \
--cc=caml-list@yquem.inria.fr \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).