0

I have a plain text file and a few random lines of data, nothing specific.. I was wondering how exactly I would go about reading this file line by line as the following pseudo code:

arrLines[]
data = open ("x.txt", read, handle)
for line in data
   if line == "stop" then stop reading
   arrLines.add(line)

close(handle)

but I want to skip the first line in the file. very simple stuff in other languages I know such as python..ect, I'm finding it very difficult to learn from the documentation because they seem to expect you to already know a whole lot of prolog before using it :/

can anyone recommend some resource where they don't expect you to know much about prolog before using it? A little more detail about what things mean and examples of using them not just a description of what it is

ace007
  • 577
  • 1
  • 10
  • 20
  • Just to point out, the documentation of SWI-Prolog is documenting this particular _implementation_ of Prolog. It is not a tutorial, nor a documentation or specification of the language, nor a textbook. You seem to be missing this point. You can try to google for "prolog tutorial", and you will get some good pointers, although I have to admit that neither is really good at explaining the complete language in an understandable manner suitable for a complete beginner. –  Mar 31 '13 at 16:06

2 Answers2

1

You can define the following predicate to read lines from a file to a list, stopping at a line consisting of nothing but "stop":

read_until_stop(File, [L|Lines]) :-
    read_line_to_codes(File, Codes), Codes \= end_of_file,
    atom_codes(L, Codes),
    L \= stop,
    !, read_until_stop(File, Lines).
read_until_stop(_, []).

File is a stream object, to get it you can use:

open(FileName, read, File).

Skip a line by reading a line. Study the sections in the manual containing read_line_to_codes, atom_codes, and open, they have a lot of useful predicates. Again, you need to know the Prolog basics.

Now this is one way to do it, another way would be to use a DCG. It would be a proper "Prolog" way of doing it. There are plenty of examples of using DCG here on stackoverflow.

If you ask yourself why is it so difficult to do this, it is because Prolog is not meant to be used as a tool for simple text processing; there are tools that are far better at it (grep, sed, awk, and of course perl).

P.S. Stackoverflow is actually a great source of code examples in Prolog. The questions tagged with SWI-Prolog will give you examples of SWI-Prolog-specific code.

0

Let me formulate the answer also in pseudocode for prolog

open the file and
find all lines which contain the string

a line is all characters until newline

I found a toturial: https://www.csupomona.edu/~jrfisher/www/prolog_tutorial/1.html

and I recommend to use apropos(append) to search for append or for open and so on. It will open the help with a usable structure.

to see what is going on while swi prolog evaluates something use gtrace

?- gtrace, append([1,2,3], [9], L).
User
  • 14,131
  • 2
  • 40
  • 59