Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Input from file to list

Status
Not open for further replies.

ombadboy

Programmer
Dec 22, 2008
2
CY
I am trying to write a program in prolog but am stuck on how to go on about doing this step.

I have a file (i.e test.txt), which has numbers in it like:

492255345
234523668
234653547

etc.. and I am interested in unifying each character (number) to a variable I have already assigned a rule in a list.

for example

list(1, [Q,W,E,R,T,Y,U..]).
list(2, [Z,X,C,V,B,N,M..]).
in this example, I am trying to write a code to assign 4 to Q, W to 9, etc..

and the 2nd row to assign Z to 2, X to 3, etc..

thnx for the help, and sorry for my bad english.
 
You can do it this way in SWI-Prolog :
Code:
work(end_of_file, _) :- !.

work(C, [H | T]) :-
	maplist(affecte, C, H),
	read_line_to_codes(current_input, C1),
	work(C1, T).


affecte(Code, Cell) :-
	char_code(Cell, Code). 
	
test :-
	Keys= [[_A,_Z,_E,_R,_T,_Y,_U,_I,_O],
               [_Q,_S,_D,_F,_G,_H,_J,_K,_L],
               [_W,_X,_C,_V,_B,_N,_M,_P,_A1]],
	see('number.txt'),
	read_line_to_codes(current_input, C1),
	work(C1, Keys),
	maplist(writeln, Keys),
	seen.
 
awesome.. works like a charm, but one small problem..

shouldnt the variables (A,B,C,D,etc) (i removed the _), unify with the variables i have declared above?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top