Re: [eclipse-clp-users] How to dynamically create a list of Eclipse structure elements from Java (Eclipse-Java embedding problem)??

From: Joachim Schimpf <joachim.schimpf_at_infotech.monash.edu.au>
Date: Wed, 30 Dec 2009 16:15:56 +1100
Kish Shen wrote:
> However, it is usually much easier to construct ECLiPSe structures from 
> within ECLiPSe.

Here is some code for reading csv files into structures.

read_csv_to_structs(File, Structs) :-
	open(File, read, Stream),
	read_lines(Stream, Structs),
	close(Stream).

     read_lines(Stream, Structs) :-
	( read_string(Stream, end_of_line, _, Line) ->
	    split_string(Line, ",", " \t", Strings),
	    ( fields_to_struct(Strings, Struct) ->
		Structs = [Struct|Structs1],
		read_lines(Stream, Structs1)
	    ;
		Nr is get_stream_info(Stream, line) - 1,
		printf(warning_output, "Couldn't convert line %d: %w%n", [Nr,Line]),
		read_lines(Stream, Structs)
	    )
	;
	    Structs = []
	).

% Adapt this predicate to convert the fields of one line
% as needed and to make the structure you want.
fields_to_struct([S1,S2,S3], mydata(X1,X2,X3)) :-
	number_string(X1, S1),
	number_string(X2, S2),
	number_string(X3, S3).


-- Joachim
Received on Wed Dec 30 2009 - 05:16:11 CET

This archive was generated by hypermail 2.2.0 : Thu Feb 02 2012 - 02:31:58 CET