GeorgeCostanza
GeorgeCostanza

Reputation: 395

Prolog: Search large list of sublists by element in sublist, then delete sublist if found

My list looks like:

 [ [005,Chester,100], [001,Bob,99], [002,Andy,77] ... ]

Where the 1st 2 elements (ID & Name) of each sublist are strings (entered with single quotes)

I'm trying to create a function that allows the user to enter a string, then searches the ID and Name field of each sublist for the entered string.

So if i enter 'Chester', the program will check for 'Chester' in both the ID and Name field of each sublist. If found, the program notifies the user and the sublist is removed. If not found, "write('Not found.')" example:

Student: Chester removed
[ [001,Bob,99],[002,Andy,77] ... ]

I'm aware of the delete predicate and can get it to work with flat lists but lists of sublists are giving me all kinds of problems. thanks

EDIT:

So far i've gotten (thanks to CapelliC)

process(8, X) :-
 nl,
 write('\tEnter student to delete: '),nl,nl,
 read_delete_info(A),
 select( [A,_,_] , X, Z),
 write(Z),
 nl, nl, menu(Z).

read_delete_info(A) :-
 write('\tStudent ID: '),
 read(A).

This will remove the sublist properly, display the new list and pass the new list back to menu.

Is it possible to edit select/3 so that if there's no match it will write "No match"? or if there is a match it will show you what the match is as well as returning the new list? thanks

Upvotes: 0

Views: 240

Answers (1)

CapelliC
CapelliC

Reputation: 60014

you can use 'if/then/else', more or less in this way

...
(  select([A,_,_], X, Z)
-> write(Z),
   etc..
;  writeln('No Match')
),
...

also, since in the first post you were willing to match either ID or Name, you could combine the test in this way

...
(  ( select([A,_,_], X, Z) ; select([_,A,_], X, Z) )
-> write(Z),
   etc..
;  writeln('No Match')
),
...

Upvotes: 1

Related Questions