[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: How to read a line of text?
From: |
Ron Stodden |
Subject: |
Re: How to read a line of text? |
Date: |
Thu, 08 Jan 2004 10:01:23 +1100 |
User-agent: |
Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.5) Gecko/20031015 |
Thanks, Alex. but your code has problems:
1. It will not deal with an empty stream. IOW the test for (not)
end of stream must immediately follow the repeat. gprolog does not
support a 'not' operator.
2. My experiments show that list decomposition is limited to the
head item only. IOW [Item|Remainder] works, but
[Item1|Item2|Remainder] will not produce your expected result.
3. Your case is limited to the processing of single lines. For
example, using your technique one could not output a list of all the
lines in a file because all data is lost at the fail of a repeat fail
combination. That would require the use of persistent storage, ie a
database or global variables.
4. The requirement to specify words as code lits is insufferably
tedious.
I will have a look at your http://apax.net/swpets/plg_p3.html
The best so far, expressed in swi-prolog, is:
read_list('locals.sort',[],Locals),
...
read_list(File,Build,List):-
open(File,read,Handle), !,
read_list_2(Handle,Build,List),
close(Handle).
read_list(File,_,_):-
format('open ~a failure',[File]),
fail.
read_list_2(Handle,Build,[Line|List]):-
not(at_end_of_stream(Handle)),
read_line_to_codes(Handle,Codes),
atom_codes(Line,Codes), !,
read_list_2(Handle,Build,List).
read_list_2(_,List,List).
Alexander V. Diemand wrote:
Hi,
I somehow missed the discussion but want to throw my 2c in:
the trick is to avoid working with "atoms" when reading files but to
use lists of characters. memory space for atoms is limited, lists
consume stack space (dynamic, released).
Since ages I am using a small toolbox in pure Prolog code. It has
recently been published:
http://apax.net/swpets/plg_p3.html
The predicate you might be interested in is read_txtline/2. It returns
a list of characters read from a (file) stream.
example:
open('file.txt',read,Str), repeat, read_txtline(Str,Line),
parseline(Line), at_end_of_stream(Str), close(Str).
% matches "From:"
parseline([70,114,111,109,58|R]) :-
do_some_thing.
% matches "To:"
parseline([84,111,58|R]) :-
do_some_thing_else.
% fallback
parseline(_).
hope that helps
Alex.
--
Ron. [Melbourne, Australia]
"If you keep a green bough in your heart, the singing bird will come"
Get Fastest Mandrake downloader, English-only, from:
http://members.optusnet.com.au/ronst/ <--- Last Change 2nd November!
- How to read a line of text?, Ron Stodden, 2004/01/03
- Re: How to read a line of text?, Vic Bancroft, 2004/01/07
- Re: How to read a line of text?, Vic Bancroft, 2004/01/07
- Re: How to read a line of text?, Ron Stodden, 2004/01/07
- Re: How to read a line of text?, Alexander V. Diemand, 2004/01/08
- Re: How to read a line of text?,
Ron Stodden <=
- Re: How to read a line of text?, Ron Stodden, 2004/01/07
- Re: How to read a line of text?, Vic Bancroft, 2004/01/07
- Re: How to read a line of text?, Ron Stodden, 2004/01/08
- Re: How to read a line of text?, Vic Bancroft, 2004/01/08
- Re: How to read a line of text?, Ron Stodden, 2004/01/08
- Re: How to read a line of text?, Fergus Henderson, 2004/01/09
- Re: How to read a line of text?, Ron Stodden, 2004/01/09
- Re: How to read a line of text?, Fergus Henderson, 2004/01/11
- Re: How to read a line of text?, Salvador Abreu, 2004/01/09