J'ai un fichier d'entrée au format suivant:Comment lire un fichier dans ocaml?
q0;q1
a;b
(q0,x);(q1;x)
Je veux avoir trois listes comme suit:
a = ["q0";"q1"];
b = ["a";"b"];
c = [("q0","x");("q1","y")];
Voici mon code:
let buf = Queue.create();;
let catfile filename =
let rec print_all_lines in_chan =
Queue.add (input_line in_chan) buf;
print_all_lines in_chan
in
let in_file = open_in filename in
try
print_all_lines in_file
with End_of_file -> close_in in_file;;
catfile "test.txt";;
let rec pvt_rec_split skipf rv str c i limit =
if (List.length rv < (limit - 1)) && (i < String.length str) then (
if String.contains_from str i c then
let o = String.index_from str i c in
pvt_rec_split skipf
(rv @ [ String.sub str i (o - i)])
str c
(skipf str c o)
limit;
else
rv @ [ String.sub str i ((String.length str) - i) ]
) else (
if i < String.length str then
rv @ [ String.sub str i ((String.length str) - i) ]
else
rv
);;
let split s c limit =
let rec pvt_skip_char s c i =
if (i >= String.length s) then (
String.length s
) else (
if ((String.get s i) == c) then (
pvt_skip_char s c (i +1)
) else (
i
)
)
in
pvt_rec_split pvt_skip_char [] s c 0 limit ;;
let a = split (Queue.take buf) ';' 100;;
let b = split (Queue.take buf) ';' 100;;
let c = split (Queue.take buf) ';' 100;;
divise fonction Fondamentalement divisé la chaîne avec délimiteur et renvoie une liste. Donc, je suis capable de générer correctement les listes a et b.
Mais dans le cas de la liste c, j'obtiens une liste de chaînes de caractères. En fait je veux une liste avec type (chaîne 'string *').
Comment faire cela?
Fonctions de base IO sont dans le [Pervasives] (http://caml.inria.fr/pub/docs/manual-ocaml /libref/Pervasives.html) module. Vous pouvez lire une ligne avec 'read_line', par exemple. Vous devrez montrer ce que vous avez déjà essayé d'obtenir de meilleurs conseils, je pense. –