0

I wrote X = [[1,2,3,4,5,6],[6,5,4,3,2,1]|...] to a file by the following code:

?-tell('test.txt'),maplist(format('~d ~d ~d ~d ~d ~d ~n'),X),told.

$cat test.txt
1 2 3 4 5 6
6 5 4 3 1 1
...

And using the following code to restore it:

?-open("test.txt",read,F),read_stream_to_codes(F,N),write(N),close(F).
N = [49, 50, 51, 52, 53, 54 ...]

what's the best way to convert N to [[1,2,3,4,5,6],[6,5,4,3,2,1]...] ?

Sincerely!

z_axis
  • 8,272
  • 7
  • 41
  • 61

1 Answers1

1

I would do one line at time:

:- use_module(library(dcg/basics)).

ints(L) --> blanks, (integer(I), ints(Is), {L = [I|Is]} ; {L = []}).

read_ints(F, L) :-
    open(F, read, S),
    file_ints(S, L),
    close(S).

file_ints(S, L) :-
    read_line_to_codes(S, Cs),
    (   Cs == end_of_file
    ->  L = []
    ;   phrase(ints(Is), Cs),
        file_ints(S, R),
        L = [Is|R]
    ).
CapelliC
  • 59,646
  • 5
  • 47
  • 90