caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* AW: [Caml-list] laconical input from a file for arrays and/or mat rices
@ 2004-02-02 15:33 Khamenia, Valery
  2004-02-02 15:41 ` Richard Jones
  0 siblings, 1 reply; 3+ messages in thread
From: Khamenia, Valery @ 2004-02-02 15:33 UTC (permalink / raw)
  To: 'Richard Jones', Khamenia, Valery; +Cc: 'caml-list@inria.fr'

Hi Rich and all,

> Probably not quite what you want, but I have a library for reading and
> writing comma-separated values (CSV) files here:
> http://www.merjis.com/developers/csv/
> http://www.merjis.com/developers/csv/ocaml-csv-1.0.1.tar.gz

thank you, it would be interesting what's the skeleton idea 
behind your code, but I can't get it after first apporoach :)

Actually, to emulate behaviour of C++ expression 
"cin >> mydoublevar" I use function cin_float 
instead of read_float:

(* ---------- START -----------*)
exception EOF of string

let max = 4096
let buf = String.create max
and wbuf = Buffer.create 64
and cin_float_i = ref 0 
and cin_float_n = ref 0

let rec scan_words i n inword =
  if i < n then
    let c = buf.[i] in
    if c!=' ' && c!='\n' && c != '\t' then begin
      Buffer.add_char wbuf c;
      scan_words (i + 1) n true
    end
    else if inword then begin
      let word = Buffer.contents wbuf in       
      Buffer.clear wbuf;
      cin_float_i:=i+1;
      cin_float_n:=n;
      float_of_string word
    end
    else scan_words (i + 1) n false
  else
    let nread = input stdin buf 0 max in
    if nread <> 0 then scan_words 0 nread inword
    else raise (EOF "reading after the end of file");;

let cin_float() = scan_words (!cin_float_i) (!cin_float_n) false;;

(* 
   test:
   Printf.printf "%f %f " (cin_float()) (cin_float()); 
*)
(* ---------- START -----------*)

However it is error-prone even for single-thread 
applications.

Indeed, after call of cin_float some symbols could megrate 
from stdin into the buffer "buf". For this reason in the 
client code after calling "cin_float" the functions like 
"read_float()" will not find all those 
symbols which have been moved into my buffer already.

Decentralized buffers :(

Now, "read_float" is actually a composition:

 float_of_string (read_line())

and "read_line" is just

 flush_stdout; input_line stdin

Thus, to avoid non-decentralized buffers one should 
access stateful "stdin" channel. 
Is "stdin" object in OCaML standardized?..

Maybe you, Rich, have a better idea.

P.S. BTW, I was impressed by a spam level in 
     ocaml-list in Jan 2004. Why posts are not 
     allowed only for a subscribers (or even 
     better for veryfied emails) ?..

--
vak

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 3+ messages in thread
* AW: [Caml-list] laconical input from a file for arrays and/or mat rices
@ 2004-02-02 16:03 Khamenia, Valery
  0 siblings, 0 replies; 3+ messages in thread
From: Khamenia, Valery @ 2004-02-02 16:03 UTC (permalink / raw)
  To: 'Richard Jones', Khamenia, Valery; +Cc: 'caml-list@inria.fr'

> Can you define the problem some more?
> Do you have lots of existing files in this particular format which
> you'd like to be able to read?  Or are you just looking for a reliable
> way to store and load your program data?

i have a lot of "R" and C/C++ code, where (especially at 
math prototyping phase) i used text ascii files to I/O 
with matrices/arrays.

Now I try to use ocaml a bit. I don't want change old code.
I'd like to use the same format. For example for matrices
I use format like here:

2 3
a b c
d e f
 
> If you've got a lot of existing files that you must read in, then
> you'll obviously need to write or find a reader for that format.

you are right, and I have posted this code in previous mail.
but i am not happy:

 1. with error-proness of my code and
 2. that these simple things kill that much time :)
 
> If you're just looking around for a suitable format for storing and
> loading your data, then locate some existing code which already does
> this and just use that. 

no-no. I am already stuck to my old format.

> [...] Another is using input/output matrix functions.

?

any link?

> You could also try converting from the space-separated format to CSV
> format using either a quick Perl script,

I was thinking about perl/python scripts.

>  or even using a spreadsheet
> program -- most spreadsheets will let you read in the space-separated
> format and write it out in CSV format, which can then be read using
> the ocaml-csv library.

conversion with spreadsheets is not reproducible and doubles 
the number of files. not good.

thanks.

--
vak

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2004-02-02 16:03 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-02-02 15:33 AW: [Caml-list] laconical input from a file for arrays and/or mat rices Khamenia, Valery
2004-02-02 15:41 ` Richard Jones
2004-02-02 16:03 AW: " Khamenia, Valery

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).