0

I was trying to take input many strings in separate lines and want to store all of them for use later.For example want to take input as follows(last line ends with a ".")-

My name is ABCD
 My name is BCDS
My name is fdada. 

How can I implement this?? Also I want to use all these strings.In java or any other language I would have made a string array and used that array to access all the three strings.

But the moment I enter 1st line it gives me false.

  • 1
    Like here: http://stackoverflow.com/questions/26005369/prolog-input-from-file just pass `user_input` to the predicate `stream_to_lines/2` instead of the open file. There is someone else asking basically the same questions as you and you should read the answers to those. –  Sep 24 '14 at 08:58
  • @ Boris Thanks I will look into it.I started trying to implement Capelli's answer and it worked well except that if I try to use a_line(L) in my code itself,it returns only the first value of L.Is there a way to get all values of L???I mean not at runtime at runtime I can press ; to get next values.I want to use the values of L in the code itself –  Sep 24 '14 at 09:11
  • @Boris I am sorry but I am new to Prolog and there are not much codes over the net to look into.I am still not able to use stream_to_lines.I can show you how I am trying to use it- `f(1) :- stream_to_lines(user_input,Lines), checkfunction(Lines). ` This is giving me following error- ERROR: f/1: Undefined procedure: stream_to_lines/2 Exception: (7) stream_to_lines(user_input, _G3732) ? –  Sep 24 '14 at 13:07
  • @Boris Ok I have editted my question now and have written what I am trying to do –  Sep 24 '14 at 13:35
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/61845/discussion-between-quantum-and-boris). –  Sep 24 '14 at 13:58
  • You have not managed to copy-paste the code correctly. you have messed up the indentation. you have switched the order of the clauses. you have not used it correctly (the predicate should be left as it is, and called with `stream_to_lines(user_input, Lines)`. Even if you get this right you have a long road ahead, and you should read a tutorial on basic prolog first. –  Sep 24 '14 at 14:52
  • @Boris Can you please explain how to use this.I am really facing difficulty understanding this.I am reading LPN book but nothing much is given there about inputs.Do you know any other book or tutorial which have some codes for reference?? –  Sep 24 '14 at 17:09
  • Go to the link in the first comment and read it. Also read all the linked SWI-Prolog documentation pages. –  Sep 24 '14 at 17:58
  • Please edit your question and make it clear what you are asking, and using what programming language. Your title, question, and tag disagree with each other. – Jongware Sep 26 '14 at 20:29

1 Answers1

0

you can use a failure driven loop, like

:- dynamic a_line/1.

read_lines :-
    retractall(a_line(__)),
    repeat,
    read_line_to_codes(user_input, L),
    assertz(a_line(L)),
    ( last(L, 0'.) ; fail ).

and then

?- read_lines.
|: My name is ABCD
|:  My name is BCDS
|: My name is fdada.
true .

the result get stored in a_line/1, so

?- a_line(L),atom_codes(A,L).
L = [77, 121, 32, 110, 97, 109, 101, 32, 105|...],
A = 'My name is ABCD' 
L = [32, 77, 121, 32, 110, 97, 109, 101, 32|...],
A = ' My name is BCDS'.
...
CapelliC
  • 59,646
  • 5
  • 47
  • 90
  • I tried your code with a few modifications trying to implement my real problem,but I am not getting the desired result.Can you see my code if possible?? –  Sep 24 '14 at 08:38
  • Actually if I want to use a_line(L) within the code itself and use all the values of L how can I do that??I am trying to compare the two lines of input and they are always coming out to be equal because the value of L is not changing. –  Sep 24 '14 at 08:44
  • 1
    I feel your problem could be worth another question - also because I'm so busy right now. Someone other could help you as well. Please remember that accepting answers it's the right way on this site to inform other users of the solution' quality, and gain reputation as well :) – CapelliC Sep 24 '14 at 08:50