Quantcast
Channel: Active questions tagged ocaml - Stack Overflow
Viewing all articles
Browse latest Browse all 531

Parsing file into Hashtable in OCaml

$
0
0

I'm trying to learn functional programming and having difficulty expressing a file parsing task functionally.Let's say I have a text file with the following format:

val_0:       <--- "header"key_0_0      <--- these keys should be set to the "header" or val0key_0_1key_0_2......val_n:     ...key_n_m

How can I end up with a hash table with all keys set to their associated value?

EDIT: My solution. Can anyone improve it?

open Core.Stdlet contains s1 s2 =        let re = Str.regexp_string s2 in        try ignore (Str.search_forward re s1 0); true        with Not_found -> falselet read_db f =         let tbl = Caml.Hashtbl.create 123456 in        let lines = In_channel.read_lines f in        let src = ref "" in        List.iter ~f:(fun g -> if contains g ":" then src := else Caml.Hashtbl.add tbl g !src) lines;        tbl

Viewing all articles
Browse latest Browse all 531

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>